# Rolling boulder code problem (complex animation)

**URL:** <https://discourse.processing.org/t/rolling-boulder-code-problem-complex-animation/30571>\
**Category:** Coding Questions\
**Created:** [June 8, 2021, 2:08pm UTC](https://discourse.processing.org/t/rolling-boulder-code-problem-complex-animation/30571 "2021-06-08T14:08:44Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![norm1](https://avatars.discourse-cdn.com/v4/letter/n/3da27b/32.png) [@norm1](https://discourse.processing.org/u/norm1)\
**Post date:** [June 8, 2021, 2:08pm UTC](https://discourse.processing.org/t/rolling-boulder-code-problem-complex-animation/30571/1 "2021-06-08T14:08:44Z")

</div>

Doing an animation that requires a boulder to roll down a hill, then go to the right of the screen, then go back left and up the hill and stop. But once I get to the BoulderRollRight I cant go roll the boulder left because the condition in the if statement in the BoulderRollRight is still being met. How can I change it so the condition is met once it reaches the end then stops so it goes left  
Here is my code:

```auto
float x = 70;
float y = 200;
int r =0;
float speedx = 2;
float speedy = 2;
void setup()
{
  myBackground();
  size(800, 500);
  boulderDownHill();
}

void myBackground()
{
  background(#16A1F5);
  fill(#624909);
  stroke(#18C949);
  strokeWeight(5);
  rect(-4, 350, 807, 155);
  stroke(#18C949);
  strokeWeight(5);
  fill(#18C949);
  triangle(-1, 180, -1, 350, 200, 350);
  stroke(#EBF00A);
  fill(#EBF00A);
  ellipse(60, 50, 40, 40);
}
void draw()
{

  myBackground();
  boulderDownHill();
  fill(#5F6239);
  strokeWeight(1);
  stroke(#5F6239);
  ellipse(-10-speedx, -10-speedy, 50, 50);
  arc(-10-speedx, -10-speedy, 60, 60, 0, PI+QUARTER_PI, PIE);
  fill(0);
  ellipse(-5-speedx, -5-speedy, 10, 10);
  ellipse(-30-speedx, -5-speedy, 10, 10);
  translate(x, y);
}
void boulderDownHill()
{
  
  if (y<330)
  {
    x=x+2;
    y=y+1.5;
    translate(x, y);
    rotate(radians(r));
    r+=2;
    translate(20, 20);
  }
  else if(y>=330){
    BoulderRollRight();
  }
}
void BoulderRollRight()
{
  if(x<750)
  {
    x=x+2;
    translate(x, y);
    rotate(radians(r));
    r+=2;
    translate(20, 20);
  }
  else if(x>=750)
  {
    boulderRollLeft();
  }
}
void boulderRollLeft()
{
  
  if(x>330)
  {
  x=x-5;
  translate(x, y);
  rotate(radians(r));
  r+=2;
  translate(20, 20);
  }
}

```

Another optional question is how would I get the boulder to slow down its movement?

---

<div class="post-metadata">

**Author:** ![glv](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/glv/32/18785_2.png) [@glv](https://discourse.processing.org/u/glv)\
**Post date:** [June 8, 2021, 2:10pm UTC](https://discourse.processing.org/t/rolling-boulder-code-problem-complex-animation/30571/2 "2021-06-08T14:10:29Z")

</div>

Hello,

Please format your code as a courtesy to the community:

[https://discourse.processing.org/faq#format-your-code](https://discourse.processing.org/faq#format-your-code)

`:)`

---

<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:** [June 8, 2021, 6:54pm UTC](https://discourse.processing.org/t/rolling-boulder-code-problem-complex-animation/30571/3 "2021-06-08T18:54:38Z")

</div>

Not sure because I didn’t test but maybe **these 2 adjustments to if/else** will work?

```auto
void BoulderRollRight()
{
  if(x<=750) // here
  {
    x=x+2;
    translate(x, y);
    rotate(radians(r));
    r+=2;
    translate(20, 20);
  }
  else // and here
  {
    boulderRollLeft();
  }

```

Please take a moment to refer to the reference for how to write **if / else-if /else** :

[https://processing.org/reference/](https://processing.org/reference/)  
Hope this helps!  
🤓

---

<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:** [June 8, 2021, 8:34pm UTC](https://discourse.processing.org/t/rolling-boulder-code-problem-complex-animation/30571/4 "2021-06-08T20:34:55Z")

</div>

> [@norm1](#):
>
> a boulder to roll down a hill, then go to the right of the screen, then go back left and up the hill and stop

You got 4 different phases / steps in your animation

So make an int variable “ **phase** ” that tells you which movement you currently want:

- like down, or right etc. - call `boulderRollLeft()` etc.
- use `switch(phase){...}` in draw() and add different stuff to x,y position to achieve this

Now once you reach certain areas on the screen (the condition in the if statement) in your code say phase++;
