# Value will not go negative (move and bounce)

**URL:** <https://discourse.processing.org/t/value-will-not-go-negative-move-and-bounce/43742>\
**Category:** Beginners\
**Created:** [January 23, 2024, 7:04pm UTC](https://discourse.processing.org/t/value-will-not-go-negative-move-and-bounce/43742 "2024-01-23T19:04:44Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![WillyGrz](https://avatars.discourse-cdn.com/v4/letter/w/c77e96/32.png) [@WillyGrz](https://discourse.processing.org/u/WillyGrz)\
**Post date:** [January 23, 2024, 7:04pm UTC](https://discourse.processing.org/t/value-will-not-go-negative-move-and-bounce/43742/1 "2024-01-23T19:04:44Z")

</div>

I’ve used almost exact code before but its not working this time. Any idea why?

float obstacleX;  
float obstacleMoveX;

…

```
square(obstacleX, obstacleY, 80);
obstacleMoveX = 2;
obstacleX = obstacleX + obstacleMoveX;
if (obstacleX >= 50) {
  obstacleMoveX = -obstacleMoveX;
}

```

---

<div class="post-metadata">

**Author:** ![scudly](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/scudly/32/5597_2.png) [@scudly](https://discourse.processing.org/u/scudly)\
**Post date:** [January 23, 2024, 7:08pm UTC](https://discourse.processing.org/t/value-will-not-go-negative-move-and-bounce/43742/2 "2024-01-23T19:08:21Z")

</div>

Is it because you keep re-initializing `obstacleMoveX` to 2 every time `draw()` gets called? Move that initialization either into `setup()` or just to the global scope.

---

<div class="post-metadata">

**Author:** ![Chrisir](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/chrisir/32/45_2.png) [@Chrisir](https://discourse.processing.org/u/Chrisir)\
**Post date:** [January 23, 2024, 9:46pm UTC](https://discourse.processing.org/t/value-will-not-go-negative-move-and-bounce/43742/3 "2024-01-23T21:46:34Z")

</div>

Example

```auto
float obstacleX = 2;
float obstacleY = 102;

float obstacleMoveX = 2;

// --------------------------------------------------------------

void setup() {
  size(1900, 1000);
}

void draw() {
  background(0);

  square(obstacleX, obstacleY, 80);

  obstacleX += obstacleMoveX;

  if (obstacleX >= 500) {
    obstacleMoveX = -obstacleMoveX; // reverse
  }
}

```

---

<div class="post-metadata">

**Author:** ![WillyGrz](https://avatars.discourse-cdn.com/v4/letter/w/c77e96/32.png) [@WillyGrz](https://discourse.processing.org/u/WillyGrz)\
**Post date:** [January 24, 2024, 4:39pm UTC](https://discourse.processing.org/t/value-will-not-go-negative-move-and-bounce/43742/4 "2024-01-24T16:39:23Z")

</div>

Thank you so much! This helped a lot!
