# App lab code fails when screen loads

**URL:** <https://forum.code.org/t/app-lab-code-fails-when-screen-loads/39591>\
**Category:** Uncategorized\
**Tags:** applab\
**Created:** [July 11, 2024, 1:13am UTC](https://forum.code.org/t/app-lab-code-fails-when-screen-loads/39591 "2024-07-11T01:13:54Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![chris](https://avatars.discourse-cdn.com/v4/letter/c/958977/32.png) [@chris](https://forum.code.org/u/chris)\
**Post date:** [July 11, 2024, 1:13am UTC](https://forum.code.org/t/app-lab-code-fails-when-screen-loads/39591/1 "2024-07-11T01:13:54Z")

</div>

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.

> **[Check out what I made](https://studio.code.org/projects/applab/haoEe3RDgrfJH452bAOzUyK_o6QKJwE7r2BffvJtTZA)**
>
> I wrote the code myself with Code.org

---

<div class="post-metadata">

**Author:** ![varrience](https://sea2.discourse-cdn.com/flex016/user_avatar/forum.code.org/varrience/32/8042_2.png) [@varrience](https://forum.code.org/u/varrience)\
**Post date:** [July 12, 2024, 7:17am UTC](https://forum.code.org/t/app-lab-code-fails-when-screen-loads/39591/2 "2024-07-12T07:17:25Z")

</div>

your issue lies within how the bouncing works for your code

```auto
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

#

---

<div class="post-metadata">

**Author:** ![chrisrodrigues182](https://sea2.discourse-cdn.com/flex016/user_avatar/forum.code.org/chrisrodrigues182/32/8985_2.png) [@chrisrodrigues182](https://forum.code.org/u/chrisrodrigues182)\
**Post date:** [July 12, 2024, 12:05pm UTC](https://forum.code.org/t/app-lab-code-fails-when-screen-loads/39591/3 "2024-07-12T12:05:23Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![varrience](https://sea2.discourse-cdn.com/flex016/user_avatar/forum.code.org/varrience/32/8042_2.png) [@varrience](https://forum.code.org/u/varrience)\
**Post date:** [July 12, 2024, 6:24pm UTC](https://forum.code.org/t/app-lab-code-fails-when-screen-loads/39591/4 "2024-07-12T18:24:55Z")

</div>

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

```auto
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
