int x; void setup(){ size(500,500); smooth(); x = 50; } void draw(){ background(200,200,200); ellipse(x,50,30,30); x+=1; }
if(x > 500){ // the above is true, do something }
if(x > width){ // the above is true, do something }
if(x > width){ // the above is true, do something }else if(x < 0){ // our x value is not greater than width but is less than 0! }else{ // the two conditions above are false, do something else }
int x; void setup(){ size(500,500); smooth(); x = 50; } void draw(){ background(200,200,200); ellipse(x,50,30,30); x+=1; if(x > width){ println("our ball is offscreen!"); } }
int x; int r; // radius int direction; int speed; void setup(){ size(500,500); smooth(); x = 50; r = 15; speed = 1; direction = 1; ellipseMode(CENTER); } void draw(){ background(200,200,200); ellipse(x,50,r*2,r*2); x+=(speed * direction); // our x position (which is in the center) // plus our radius to achieve the right edge if(x+r > width){ direction = -1; } }
int x; int r; // radius int direction; int speed; void setup(){ size(500,500); smooth(); x = 50; r = 15; speed = 1; direction = 1; ellipseMode(CENTER); } void draw(){ background(200,200,200); ellipse(x,50,r*2,r*2); x+=(speed * direction); if(x+r > width){ direction = -1; }else if(x-r < 0){ direction = 1; } }
int x; int y; int r; // radius int direction_x; int direction_y; int speed; void setup(){ size(500,500); smooth(); x = 50; y = 50; r = 15; speed = 1; direction_x = 1; direction_y = 1; ellipseMode(CENTER); } void draw(){ background(200,200,200); ellipse(x,y,r*2,r*2); x+=(speed * direction_x); y+=(speed * direction_y); if(x+r > width){ direction_x = -1; }else if(x-r < 0){ direction_x = 1; } if(y+r > height){ direction_y = -1; }else if(y-r < 0){ direction_y = 1; } }
int x; int y; int r; // radius int direction_x; int direction_y; int speed; void setup(){ size(500,500); smooth(); x = int(random(0,width)); y = int(random(0,height)); r = 15; speed = 1; direction_x = 1; direction_y = 1; ellipseMode(CENTER); } void draw(){ background(200,200,200); ellipse(x,y,r*2,r*2); x+=(speed * direction_x); y+=(speed * direction_y); if(x+r > width){ direction_x = -1; }else if(x-r < 0){ direction_x = 1; } if(y+r > height){ direction_y = -1; }else if(y-r < 0){ direction_y = 1; } }
// ... in the setup function x = int(random(r,(width-r))); y = int(random(r,(height-r))); // ... rest of our code
int x; int y; int r; // radius int direction_x; int direction_y; int speed; void setup(){ size(500,500); smooth(); x = int(random(r,(width-r))); y = int(random(r,(height-r))); r = 15; speed = 5; direction_x = 1; direction_y = 1; ellipseMode(CENTER); fill(random(0,255),random(0,255),random(0,255)); } void draw(){ background(100,100,100); ellipse(x,y,r*2,r*2); x+=(speed * direction_x); y+=(speed * direction_y); if(x+r > width){ direction_x = -1; }else if(x-r < 0){ direction_x = 1; } if(y+r > height){ direction_y = -1; }else if(y-r < 0){ direction_y = 1; } }