That won’t work when you have more than one ball.
Instead use variables: ellipse (x5,y5,100,100);
Now you can use dist() to find out on which ball the mouse has been clicked. That one is the active ball. Now you can say x5=mouseX; y5=mouseY;
So whenever mousePressed () is called check dist to all balls; when near, set activeBallNumber to 5; or whatever. Make it a global variable.
Yeah and in draw() when activeBallNumber is > 0 use if(activeBallNumber==5) { etc. to distinguish which ball to move.
In mousereleased say activeBallNumber=-1;
Before setup: int activeBallNumber=-1;
(
In fact that is hard when you don’t use an array
So store the balls in an array and loop over it. Makes your life easier
)
int activeBallNumber=-1;
float x1=100, y1=200,
x5=200, y5=300;
void setup() {
size(1000, 1000);
noStroke ();
background(255, 255, 255);
}
void draw() {
background(255);
fill(255, 0, 0);
ellipse (x5, y5, 100, 100);
fill(0, 0, 255);
ellipse(x1, y1, 100, 100); //
if ( activeBallNumber >=0) {
if (activeBallNumber ==1) {
x1=mouseX;
y1=mouseY;
} else if (activeBallNumber ==5) {
x5=mouseX;
y5=mouseY;
}
}
}
//----------------------------------------------------------------
void mousePressed() {
if (dist(mouseX, mouseY, x1, y1)< 110) {
activeBallNumber= 1;
} else if (dist(mouseX, mouseY, x5, y5)< 110) {
activeBallNumber=5;
}
}
void mouseReleased() {
activeBallNumber=-1;
}
//