How to listen to Flash events that don’t bubble

I learned a little trick the other week and thought I’d share. Sometimes you might need to listen to a non-bubbling event “up the chain” somewhere, but of course, since it is not bubbling, that can be hard. The trick is that event events that don’t have bubbling still have the capture phase, so if you flip that capture flag to true in the event listener then you can now listen to any events dispatched by any child views whether they are bubbling or not.

Example:

// listen for some event (this one is even custom) that does not bubble
// make sure to set true on capture
stage.addEventListener(“customNonBubbling”, myListener, true);

<mx:Button id=”myButton” click=”dispatchEvent(new Event(‘customNonBubbling’))”/>

Note: this only works on display objects as far as I know. Capture and bubbling don’t apply to objects that are not on the display list.



17 Responses to “How to listen to Flash events that don’t bubble”

  1. Tony Fendall says:

    That’s awesome. Thank you!

  2. Tahir Ahmed says:

    I have a question and will try to ask using an example.

    Suppose, I have a main class called and then I have something called as a child of and then I have as a child of and they are all added to display list.

    Now, if dispatches event and the is set to and I have a listener attached to all three classes (i.e. , and ) for that particular event type , what would be the event flow order?

    Say I had a in the listener functions in those three classes, what would be the trace order?

  3. Jacob Wright says:

    You can learn more about event handling here: http://www.adobe.com/devnet/actionscript/articles/event_handling_as3_03.html

    If you’re listening on the capture phase then the closer you are to root, the sooner you’ll handle the event. If you’re just listening normally then the closer you are to the target the sooner you’ll handle the event.

  4. lucas says:

    Nice tip thx !

  5. [...] 3.      Player Events, Custom Events, Event BubblingI can’t imagine where flash player would be without frames and mouse clicks.http://www.adobe.com/devnet/actionscript/articles/event_handling_as3_03.htmlhttp://livedocs.adobe.com/flex/3/langref/flash/events/package-detail.htmlhttp://www.tink.ws/blog/custom-events-in-as-30-dont-forget-to-override-the-clone-method/http://jacwright.com/blog/70/how-to-listen-to-flash-events-that-dont-bubble/ [...]

Leave a Reply