How to create mouse event listeners for objects?

I find it difficult to understand how events and event listeners work through the default advancedEvents example.

So in my project, I made a custom object flyingBall and each flyingBall is continuously flying around the screen. Each flyingBall has its own variables which define its location, eg. myFlyingBall’s position can be obtained through myFlyingBall.x and myFlyingBall.y. I want to create a mouse event listener specifically for each flyingBall that will detect when the mouse is inside the ball and dragging it, so as to allow the user to move the balls around directly.

How do I do that?

In the ball.cpp file (or whatever you called your ball class) you can create a boolean to check if the x and y of the mouse are within the radius of the ball that has for coordinates position.x and position.y:

bool inside(float x, float y) {
float dist = ofDist(x, y, position.x, position.y);
if(dist < radius)
{
return true;
}
else
{
return false;
}
}

then in your testApp.cpp under mouse pressed you need to check if the mouse is inside the ball and then do whatever you want it to do:

for(int i=0; i<balls.size(); i++)
{
if(balls[i].inside(x, y))
{
// do something
}
}

i hope this helps.