App lab code fails when screen loads

Hey all,

Has anyone run into an issue with some code not running correctly after using a click event to load a screen? I have image movement that works with a button press if the screen is set to default, but if I load the screen via click event and setscreen, the code then doesn’t run as expected. Any help/ideas would be great.

your issue lies within how the bouncing works for your code

boxMove = timedLoop(1000/60, function() {
    if (xPos < 0 || rightEdge > screenWidth) {
      xDir= -xDir;
      // dictates how to reset the corner
      var corner = (rightEdge >= screenWidth);
      setProperty("net2", "x", corner * 320)
      // console.log("it's negative");
    } 
    setProperty ("net2","x",xPos + (5 * xDir));
    xPos = getXPosition("net2");
    rightEdge = xPos + getProperty("net2","width");
    // console.log("It's Positive!");
  });

this example fixes it by accounting for when it’s out of bounds and resets the position so it no longer satisfies the conditional to force it to flip directions for example if my current direction is -1 but I’m moving 5 it’s -5 every single time, so i could wind up with an xPos of -5 and since your original conditional was looking for <= this would force it to spiral between these two positions 0 & -5 because both satisfy for a flip, i also added insurance to set the position back to a safe number each time it hits the border

Varrience

Thanks so much. I’ll check it out in detail later today. Any idea why it was running fine if I set the screen as default instead of having it loaded? I found that really odd. I was thinking maybe it had something to do with the original set position of the net, but I couldn’t seem to figure it out.

most likely has to do with the element not existing on the current screen making the default behavior return 0 instead of the actual element position I’ll refactor it for you so it doesn’t make the inital jump when starting

var xPos, boxMove;
var xDir = 1;
var screenWidth = 320;
var rightEdge = xPos + getProperty("net2", "width");

onEvent("start", "click", function () {
  setPosition("puck2", 192, 316);
  if (boxMove) {
    console.log("box be moving");
  }
  else {
    xPos = getXPosition("net2");
    boxMove = timedLoop(1000 / 60, function () {
      if (xPos <= 0 || rightEdge >= screenWidth) {
        xDir = -xDir;
        console.log("it's negative");
      }
      setProperty("net2", "x", xPos + (5 * xDir));
      xPos = getXPosition("net2");
      rightEdge = xPos + getProperty("net2", "width");
      console.log("It's Positive!");
    });
  }
});
onEvent("stop", "click", function () {
  if (typeof boxMove === "number") {
    stopTimedLoop(boxMove);
    boxMove = null;
  } else {
    console.log("net isn't moving silly")
  }
});

hopefully there are no further issues

Varrience

It could be related to the initial position or loading order