# Rolling boulder code problem (complex animation)

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:

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);
r+=2;
translate(20, 20);
}
else if(y>=330){
BoulderRollRight();
}
}
void BoulderRollRight()
{
if(x<750)
{
x=x+2;
translate(x, y);
r+=2;
translate(20, 20);
}
else if(x>=750)
{
boulderRollLeft();
}
}
void boulderRollLeft()
{

if(x>330)
{
x=x-5;
translate(x, y);
r+=2;
translate(20, 20);
}
}

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

Hello,

https://discourse.processing.org/faq#format-your-code

:)

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

void BoulderRollRight()
{
if(x<=750) // here
{
x=x+2;
translate(x, y);
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/
Hope this helps!

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++;