Nothing happens

edited December 2017 in Questions about Code

Hy,

I'm trying to make a point oscillate, starting from the center of a circle and moving the right side, then moving to the opposite left side and then moving to the center. So there are 3 times : moving from center to the right side , moving from the right side to the left side , moving from the left side to the center, and looping all of this. But nothing happens.

Here's my code:

int xPos;
int speed = 2;
int quadrant = 1;

void setup(){
  size(800,800);
  xPos = width/2;
}

void draw(){
  background(127);
  ellipse(width/2, width/2, width/2, width/2);

  movexPos();
  display();
}

void movexPos(){
  while(quadrant == 1){
    if((xPos >= width/2) && (xPos < (width/2) + (width/4))){
      xPos = xPos + speed;
    } else quadrant = 2;
      }

  while(quadrant == 2){
    xPos = xPos - speed;
    if(xPos < (width/2) - (width/4)){
        quadrant = 3;
      }
    }

  while(quadrant == 3){
      xPos = xPos + speed;
      if(xPos >= width/2){
        quadrant = 1;
      }
    }
  }

void display(){
  stroke(0);
  strokeWeight(3);
  point(width/2, width/2);
  point(xPos, width/2);
}
Tagged:

Answers

  • Have you tried debugging your code? Which line of code behaves differently from what you expected?

  • I tried to only make the point move from the center to the right side, but it still didn't work. So I removed the while loop and it worked. But I need the while loop to make the point oscillate completly. The problem maybe comes from the while loop but I don't know why. I don't get any compilation errors.

  • I don't think you want those to be while loops. Keep in mind that the draw() function is called 60 times per second. You should use that to animate your sketch instead of a while loop.

    If you call a while loop inside draw(), then the only thing you see is the result of the while loop. You don't see each individual step inside the loop, because stuff is only drawn to the screen after draw() completes.

  • Ok it works now, thx.

Sign In or Register to comment.