PDA

View Full Version : Better to use one mouse listener for stage, or one for every object?



doomtoo
July 31st, 2009, 03:10 PM
There's two possible ways I can see of trying to detect when the mouse is over/clicked something.

1. Add a mouse listener to the stage, then determine what the mouse is over depending on what is on the screen at the time/visible.

2. Adding mouse listeners for each object that is visible, and removing them when the object is no longer visible

Number 2 is much easier to implement, but would having a lot of mouse listeners cause it to be slower than using my own code to try to figure out which object it is over, or would the built in code be faster?

flyingmonkey456
July 31st, 2009, 04:28 PM
There's two possible ways I can see of trying to detect when the mouse is over/clicked something.

1. Add a mouse listener to the stage, then determine what the mouse is over depending on what is on the screen at the time/visible.

2. Adding mouse listeners for each object that is visible, and removing them when the object is no longer visible

Number 2 is much easier to implement, but would having a lot of mouse listeners cause it to be slower than using my own code to try to figure out which object it is over, or would the built in code be faster?

create an array containing every object that you need to test. then you can do getObjectsUnderPoint and put all of those in an array. just compare the arrays and you'll know what's under the mouse.