I am making a project where the ball follows when i click the mouse, but that is not happening. Can anyone help?

I do not know how to fix the blue color only in the box, also I am trying to use mouse clicked, so that when the mouse is clicked the ball follows it and returns back to the rectangle, but that is not happening. Can someone help?

float x = 25;
float y = 25;

float dX = 0;
float dY = 0;

float home = 10;
void setup(){
size(1080, 768); // screen size

}//end setup

void draw(){
background(0);

fill(255); //white box

rect(0,0,200,200); // is the dog house

changecolour();
if(x > dX && x < dX+200){
fill(0,0,255);
}

ellipse(x, y, 50, 50); // is the dog

goToCrusor();
goToSquare();

}//end draw

void calStuff(){

float angle = atan2(mouseY - y, mouseX - x);

x = x + 9 * cos(angle);
y = y + 9 * sin(angle);

}//end calStuff

void goToCrusor(){

if(mousePressed == true){
calStuff();
}
}
//end goToCrusor
void goToSquare(){
if(mousePressed == false){
float angle = atan2(home - y,home - x);

x = x + 9 * cos(angle);
y = y + 9 * sin(angle);

if(x < 25){
x = 25;
}
if(y < 25){
y = 25;
}

}
}// end goToSquare

void changecolour(){ // changes colour when the ball touches crusor
float d = dist(mouseX,mouseY,x,y);

if (d < 25){
fill(255,0,0); // r g b
}
else{
fill(0,255,0);
}
}

I think you first have to use the mousePressed function, before you can do your check.

void mousePressed (){
if (mousePressed == true){

}
}

You can find a fully working example in the examples section for processing which does this exact thing, i can remember the name of the sketch though.

Thanks a lot, yea I got it now, it was in one of the examples!

1 Like