Game Development Stack Exchange is a question and answer site for professional and independent game developers. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

What is the best way to handle MouseEvents?

Should I add an event on each MovieClip I want to work with, or a general Event at a holder MovieClip (or even at the stage) with internal control from the function?

I generaly see people here at work using one Event for each MovieClip, but all linked to the same funcion that handles them in general, is that a good thing? "target" and "currentTarget" seem a bit weird to me at first, but I think I'm starting to get it.

share|improve this question
up vote 2 down vote accepted

I recommend adding events to the MovieClips of interest and not a general handler on the stage that you then sort into different objects. The massive switch statement or whatever that you'd be writing would be annoying to maintain and not really gain you anything.

Indeed, I sometimes put in extra invisible Sprites solely for the purpose of managing which objects have event listeners applied. The reason is because sometimes mouse handlers get confused and behave oddly when you apply a mouse event listener to the child of another object with a mouse event listener; it's cleaner to deal with when all objects with event listeners are on completely separate branches of the display hierarchy.

For example, if I have a button attached to a MovieClip and I want both the button and MovieClip to receive separate mouse events, what I'll do is make an extra sprite and attack both the button and the MovieClip to that.

I want to add the caveat here that it's more efficient for processing if there is only one listener for each event on an object. This is kinda premature optimizations so I wouldn't stress over it too much, but if you have multiple functions that you want to happen in response to an event then it's better to have a single event listener that calls the multiple functions.

This is a slightly different situation than what you asked (a single listener on each of multiple objects vs. multiple listeners on one object) but it's related.

share|improve this answer
Thanks for the overview, I agree with your arguments and now I have a better idea on how do structure my future code. – Galvas May 29 '12 at 14:34

There's an official document from Adobe Optimizing Performance for the Flash Platform - Help - Adobe that says on page 37 that you should call addEventListener() only once:

The code is simplified and much more optimized, with only one call to the addEventListener() method on the parent container. Listeners are no longer registered to the Apple instances, so there is no need to remove them when an apple is clicked

See pages 36 and 37 for a very good example.

share|improve this answer
Thanks for that document, I hadn't seen that before. Their example is only practical for pretty simple situations however (eg. a bunch of target images spewing from one emitter). For any reasonably complex display heirarchy (eg. the UI of most applications) then the tiny performance gain is not worth the much harder to maintain code. For example, if your main screen instantiates a menu of buttons, then you'll have to write a getter or something to communicate back to the main screen all the buttons in the menu. ugh – jhocking May 25 '12 at 1:17
Thanks for sharing that document too. – Galvas May 29 '12 at 14:35

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.