# How do I stop an if-statement from carrying over to the next function

**URL:** <https://discourse.processing.org/t/how-do-i-stop-an-if-statement-from-carrying-over-to-the-next-function/11309>\
**Category:** Beginners\
**Created:** [May 16, 2019, 4:48pm UTC](https://discourse.processing.org/t/how-do-i-stop-an-if-statement-from-carrying-over-to-the-next-function/11309 "2019-05-16T16:48:19Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![debxyz](https://avatars.discourse-cdn.com/v4/letter/d/58956e/32.png) [@debxyz](https://discourse.processing.org/u/debxyz)\
**Post date:** [May 16, 2019, 4:48pm UTC](https://discourse.processing.org/t/how-do-i-stop-an-if-statement-from-carrying-over-to-the-next-function/11309/1 "2019-05-16T16:48:19Z")

</div>

**I would like the ball to bounce along the x-axis; the donut to bounce along the y-axis.**

Currently the ball movement is fine.

The donut movement is following the if-statement for the ball as well as the donut.

**How do I stop donut from executing the ball if-statement? And follow only the donut if-statement?**

**I tried using pushMatrix() / popMatrix() & then pushStyle() / popStyle above and below the code for displayBall().** And after reading the Processing Reference and seeing the results understand why this is not a viable solution.

\*\*\ ***Please note,** I’ll put the ball and donut each into their own class next.  
But I want to understand how to stop code to carry over in an instance like this one.

```auto
float x = 0;
float y = 50;
int r = 25;
float speed = random(1, 3);

void setup() {
  size(400, 400);
}
void draw() {
  background(255);
  displayBall();
  displayDonut();
}

void displayBall() {

  noFill();
  strokeWeight(3);
  ellipse(x+r, 50, r*2, r*2);

  x = x + speed;
  if ((x >= width-r*2)||(x < 0)) {
    speed = speed *-1;
  }
}

void displayDonut() {

  noFill();
  strokeWeight(3);

  ellipse(x+(r*2), y, r*4, r*4);
  ellipse(x+(r*2), y, r*2, r*2);

  y = y + speed;
  if ((y >= height-r*2)||(y < r*2)) {
    speed = speed *-1;
  }
}

```

---

<div class="post-metadata">

**Author:** ![quark](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/quark/32/26_2.png) [@quark](https://discourse.processing.org/u/quark)\
**Post date:** [May 16, 2019, 4:56pm UTC](https://discourse.processing.org/t/how-do-i-stop-an-if-statement-from-carrying-over-to-the-next-function/11309/2 "2019-05-16T16:56:33Z")

</div>

It is nothing to do with the if statement, both the ball and donut use the same variable `x` for their horizontal position.

---

<div class="post-metadata">

**Author:** ![quark](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/quark/32/26_2.png) [@quark](https://discourse.processing.org/u/quark)\
**Post date:** [May 16, 2019, 5:00pm UTC](https://discourse.processing.org/t/how-do-i-stop-an-if-statement-from-carrying-over-to-the-next-function/11309/3 "2019-05-16T17:00:53Z")

</div>

Change the code in the `displayDonut()` method to

```auto
  ellipse(r*2, y, r*4, r*4);
  ellipse(r*2, y, r*2, r*2);

```

---

<div class="post-metadata">

**Author:** ![debxyz](https://avatars.discourse-cdn.com/v4/letter/d/58956e/32.png) [@debxyz](https://discourse.processing.org/u/debxyz)\
**Post date:** [May 16, 2019, 5:13pm UTC](https://discourse.processing.org/t/how-do-i-stop-an-if-statement-from-carrying-over-to-the-next-function/11309/4 "2019-05-16T17:13:05Z")

</div>

Thank you @quark! That worked! 🙂

But now the ball is not going all the way to the right edge.  
I’ve tried a few calculations with width / and radius, latest below:

```auto
void displayBall() {

  noFill();
  strokeWeight(3);
  ellipse(x+r, 50, r*2, r*2);

  x = x + speed;
  if ((x >= width - r)||(x < 0)) {
    speed = speed *-1;
  }
}

```

But no luck. What am I overlooking here?

---

<div class="post-metadata">

**Author:** ![debxyz](https://avatars.discourse-cdn.com/v4/letter/d/58956e/32.png) [@debxyz](https://discourse.processing.org/u/debxyz)\
**Post date:** [May 16, 2019, 5:43pm UTC](https://discourse.processing.org/t/how-do-i-stop-an-if-statement-from-carrying-over-to-the-next-function/11309/5 "2019-05-16T17:43:35Z")

</div>

Nevermind. I figured it out.

---

<div class="post-metadata">

**Author:** ![quark](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/quark/32/26_2.png) [@quark](https://discourse.processing.org/u/quark)\
**Post date:** [May 16, 2019, 8:28pm UTC](https://discourse.processing.org/t/how-do-i-stop-an-if-statement-from-carrying-over-to-the-next-function/11309/6 "2019-05-16T20:28:53Z")

</div>

For others that might be interested it was the variable speed was reversed in the displayDonut method before the ball reached the right hand side. I don’t know how you solved it but here is mine 😄

```auto
float x = 0;
float y = 50;
int r = 25;
float speedX, speedY;

void setup() {
  size(400, 400);
  speedX = speedY = random(1,3);
}

void draw() {
  background(255);
  displayBall();
  displayDonut();
}

void displayBall() {

  noFill();
  strokeWeight(3);
  ellipse(x + r, 50, r*2, r*2);

  x = x + speedX;
  if ((x > width - 2*r) || (x < 0)) {
    speedX = speedX * -1;
  }
}

void displayDonut() {

  noFill();
  strokeWeight(3);

  ellipse(r*2, y, r*4, r*4);
  ellipse(r*2, y, r*2, r*2);

  y = y + speedY;
  if ((y >= height-r*2)||(y < r*2)) {
    speedY = speedY *-1;
  }
}

```

---

<div class="post-metadata">

**Author:** ![debxyz](https://avatars.discourse-cdn.com/v4/letter/d/58956e/32.png) [@debxyz](https://discourse.processing.org/u/debxyz)\
**Post date:** [May 16, 2019, 8:46pm UTC](https://discourse.processing.org/t/how-do-i-stop-an-if-statement-from-carrying-over-to-the-next-function/11309/7 "2019-05-16T20:46:14Z")

</div>

Many thanks @quark for your help!
