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.

21 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. 8only.cn-创见未来 » Blog Archive » 成为高深Flasher,你够资格了吗? Says:

    […] 真不知道那个Flash不用处理帧和鼠标点击。 http://www.adobe.com/devnet/actionscript/articles/event_handling_as3_03.html http://livedocs.adobe.com/flex/3/langref/flash/events/package-detail.html http://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/ […]

  5. lucas Says:

    Nice tip thx !

  6. Norris's Landscape » [转]Flex开发者需要知道的10件事 Says:

    […] http://jacwright.com/blog/70/how-to-listen-to-flash-events-that-dont-bubble/ […]

  7. Flash开发者需要知道的10件事 - Geeky Derek Says:

    […] http://jacwright.com/blog/70/how-to-listen-to-flash-events-that-dont-bubble/ […]

  8. Flash开发者需要知道的10件事 | Adobe Show Center Says:

    […] http://jacwright.com/blog/70/how-to-listen-to-flash-events-that-dont-bubble/ […]

  9. Flash开发者需要知道的10件事[转] « Leo Says:

    […] http://jacwright.com/blog/70/how-to-listen-to-flash-events-that-dont-bubble/ […]

  10. visioncoding in english » Blog Archive » 10 Things Every Senior Flash Developer Should Know Says:

    […] 3.      Player Events, Custom Events, Event Bubbling I can’t imagine where flash player would be without frames and mouse clicks. http://www.adobe.com/devnet/actionscript/articles/event_handling_as3_03.html http://livedocs.adobe.com/flex/3/langref/flash/events/package-detail.html http://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/ […]

  11. Flash和Flex开发者都需要知道的20 件事 | kiss the rain Says:

    […] http://www.adobe.com/devnet/actionscript/articles/event_handling_as3_03.html http://livedocs.adobe.com/flex/3/langref/flash/events/package-detail.html http://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/ […]

  12. 10 Things Every Senior Flash Developer Should Know – DIDIPUB Says:

    […] 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/ […]

  13. Flash开发者需要知道的10件事 | My Sky Says:

    […] http://jacwright.com/blog/70/how-to-listen-to-flash-events-that-dont-bubble/ […]

  14. 10 Things Every Senior Flash Developer Should Know | johnlindquist.com Says:

    […] http://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/   4.      Statements, Keywords, and Directives You should never be […]

  15. Top 20 Things every Senior Flash Developer should know – Confessions of an Flash Addict Says:

    […] 3. Player Events, Custom Events, Event Bubbling I can’t imagine where flash player would be without frames and mouse clicks. http://www.adobe.com/devnet/actionscript/articles/event_handling_as3_03.html http://livedocs.adobe.com/flex/3/langref/flash/events/package-detail.html http://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/ […]

  16. Flash开发者需要知道的10件事 | We are coding Says:

    […] http://jacwright.com/blog/70/how-to-listen-to-flash-events-that-dont-bubble/ […]

  17. Błażej Adamczyk Blog » 10 Rzeczy które każdy senior flash developer powinien wiedzieć Says:

    […] 3.      Wykonywanie zdarzeń (Player Events, Custom Events, Event Bubbling) Chyba jedyny język programowania który ma listwę czasową, klatki oraz nasłuchiwanie zdarzeń. http://www.adobe.com/devnet/actionscript/articles/event_handling_as3_03.html http://livedocs.adobe.com/flex/3/langref/flash/events/package-detail.html http://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/ […]

  18. 10条Flash AS3开发人员实用的简单细节事情 | AS3前端 Says:

    […] http://jacwright.com/blog/70/how-to-listen-to-flash-events-that-dont-bubble/ […]

  19. 10条Flash AS3开发人员实用的简单细节事情 | Riaelite's Blog Says:

    […] http://jacwright.com/blog/70/how-to-listen-to-flash-events-that-dont-bubble/ […]

  20. Flash开发者需要知道的10件事 « Rakuten Blog Says:

    […] http://jacwright.com/blog/70/how-to-listen-to-flash-events-that-dont-bubble/ […]

  21. 10 Things Every Senior Flash Developer Should Know Says:

    […] http://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/ 4. Statements, Keywords, and Directives You should never be surprised by a new “word” in as3. […]