How do I make the circle appear from right to left?

I have been working on code for so many hours straight, I'm starting to loose my mind.

Trying to get the circle appear from right to left, as opposed from left to right. It's a simple answer, but I cannot figure it out right now.

// this time, we get the circle to "warp" back to the left side


PImage bg;

// here, the number 50 is half of the circle's diameter
float x = 500;


//--------------------------------------------------------
void setup() {
  size(852, 480);
  noFill();
  stroke(255); // white
  strokeWeight(2);

bg = loadImage("bg.jpg");

}

//--------------------------------------------------------
void draw() {
  background(bg); // background picure
  ellipse(x, 150, 100, 100);


  // by adding the circle's radius to the width of the window, 
  // we allows it to "leave" the screen on the right side 
  // and warp only when it's no longer visible 
  if ( x > width + 50 ) {  
    x = x - 2; 
  }    
  else {
    // move the circle back to the left side of the window
    x = 450;  
  }
}
Tagged:

Answers

  • Your if is wrong! You want to check the Ball against the left side, not the right side

  • I know, the if statement is wrong, I don't know how to fix it. There's a flip of a negative or something, I can't figure it out.

  • edited May 2017
    float x = 500;
    PImage bg;
    float dX = 2;
    
    void setup() {
      size(852, 480);
      frameRate(100);
      bg = loadImage("bg.jpg");
    }
    
    void draw() {
      background(bg);
      noFill();
      stroke(255);
      strokeWeight(2);
      ellipse(x, 150, 100, 100);
      if (x > width) {
        dX = -dX;
      }
      if (x < -50) {
        x = 450;
      }
      x = x -dX;
    }
    
  • @NoName please correct the format of the code above

Sign In or Register to comment.