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

**URL:** <https://discourse.processing.org/t/i-am-making-a-project-where-the-ball-follows-when-i-click-the-mouse-but-that-is-not-happening-can-anyone-help/26352>\
**Category:** Coding Questions\
**Tags:** homework\
**Created:** [December 18, 2020, 12:16am UTC](https://discourse.processing.org/t/i-am-making-a-project-where-the-ball-follows-when-i-click-the-mouse-but-that-is-not-happening-can-anyone-help/26352 "2020-12-18T00:16:53Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![880597](https://avatars.discourse-cdn.com/v4/letter/8/51bf81/32.png) [@880597](https://discourse.processing.org/u/880597)\
**Post date:** [December 18, 2020, 12:16am UTC](https://discourse.processing.org/t/i-am-making-a-project-where-the-ball-follows-when-i-click-the-mouse-but-that-is-not-happening-can-anyone-help/26352/1 "2020-12-18T00:16:53Z")

</div>

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);  
}  
}

---

<div class="post-metadata">

**Author:** ![Divitiacus](https://avatars.discourse-cdn.com/v4/letter/d/848f3c/32.png) [@Divitiacus](https://discourse.processing.org/u/Divitiacus)\
**Post date:** [December 18, 2020, 2:52am UTC](https://discourse.processing.org/t/i-am-making-a-project-where-the-ball-follows-when-i-click-the-mouse-but-that-is-not-happening-can-anyone-help/26352/2 "2020-12-18T02:52:06Z")

</div>

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

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

> **[mousePressed() \\ Language (API) \\ Processing 3+](https://processing.org/reference/mousePressed_.html)**

---

<div class="post-metadata">

**Author:** ![paulgoux](https://avatars.discourse-cdn.com/v4/letter/p/b9bd4f/32.png) [@paulgoux](https://discourse.processing.org/u/paulgoux)\
**Post date:** [December 18, 2020, 6:58am UTC](https://discourse.processing.org/t/i-am-making-a-project-where-the-ball-follows-when-i-click-the-mouse-but-that-is-not-happening-can-anyone-help/26352/3 "2020-12-18T06:58:41Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![880597](https://avatars.discourse-cdn.com/v4/letter/8/51bf81/32.png) [@880597](https://discourse.processing.org/u/880597)\
**Post date:** [December 27, 2020, 11:17pm UTC](https://discourse.processing.org/t/i-am-making-a-project-where-the-ball-follows-when-i-click-the-mouse-but-that-is-not-happening-can-anyone-help/26352/4 "2020-12-27T23:17:41Z")

</div>

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