//Animate ball //variables boolean button = false; //button state int x = 25; int y = 25; int w = 50; int h = 35; int ball_x = 100; int max_x = 150; int speed = 5; float pressed = 255; float unpressed = 0; //set ip void setup() { size(480, 270); } void draw() { // The button is pressed if (mouseX,mouseY) is inside the rectangle and mousePressed is true. if (mouseX > x && mouseX < x+w && mouseY > y && mouseY < y+h && mousePressed) { button = true; } else { button = false; } if (button) { background(pressed); stroke(0); //button is pressed, add speed to ball_x until max_x while(ball_x < max_x){ if (!(ball_x > width) || (ball_x < 0)) { // If the object is not on either edge, ball_x = ball_x + speed; } } } else { background(unpressed); stroke(255); //button is not pressed, reset ball_x for (int i = max_x; i >= 100; i -= speed) { ball_x = i; } } fill(175); rect(x,y,w,h); // Display circle at ball_x location stroke(0); fill(175); ellipse(ball_x, 100, 32, 32); }