Can you help me repair it?

int x=500;
int o=600;
void setup() {
 size(1000,1000);
 strokeWeight(2);
}

void draw() {
background(100,150,200);
line(500,200,x,700);
ellipse(x,700,200,200);
ellipse(x,700,170,170);
ellipse(x,700,140,140);
ellipse(x,700,100,100);
ellipse(x,700,70,70);
if(x<600){
  x=x+1;}
else if(x==600 || x<500 ){
  x=o;
  o=o-1;
  x=x-1;}
}

Good morning Soloh,

Could you please edit your post and format the code? Open your sketch in Processing, click on Edit / Auto Format, and copy the formatted code In your post. In your post click on < /> Preformatted text and paste it on the right position. The end result should look something like:

int x=500;
int o=600;
// ...
// rest of your code
// ...
  x = x - 1;}
}

About your issue. At the end of void draw() you’ve placed an if/else if statement. The problem is that once x is higher than 600 and enters the else if part, x gets reduced. This means that in the following draw loop x is lower than 600 again and it enters the if again.
As a result your animation keeps rapidly switching between if and else if around x = 600. Do you understand now why your issue is happening?

There are several ways to solve this, but if you want to maintain your current structure I’d go for a boolean that keeps track of the swing direction. For instance, if it’s true it should swing to the right, and once it reaches x = 600 it becomes false. Using that value, you could adjust your else if conditions.

4 Likes