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.
March 3rd, 2008 at 6:53 pm
That’s awesome. Thank you!
September 29th, 2009 at 6:58 am
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?
September 29th, 2009 at 2:54 pm
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.
December 27th, 2009 at 11:24 pm
[...] 真不知道那个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/ [...]
February 1st, 2010 at 3:37 pm
Nice tip thx !
February 18th, 2010 at 11:28 pm
[...] http://jacwright.com/blog/70/how-to-listen-to-flash-events-that-dont-bubble/ [...]
February 19th, 2010 at 3:14 pm
[...] http://jacwright.com/blog/70/how-to-listen-to-flash-events-that-dont-bubble/ [...]
February 19th, 2010 at 7:29 pm
[...] http://jacwright.com/blog/70/how-to-listen-to-flash-events-that-dont-bubble/ [...]
February 20th, 2010 at 11:57 pm
[...] http://jacwright.com/blog/70/how-to-listen-to-flash-events-that-dont-bubble/ [...]
February 22nd, 2010 at 4:27 am
[...] 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/ [...]
March 10th, 2010 at 11:26 pm
[...] 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/ [...]
April 15th, 2010 at 3:38 am
[...] 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/ [...]
April 23rd, 2010 at 8:03 pm
[...] http://jacwright.com/blog/70/how-to-listen-to-flash-events-that-dont-bubble/ [...]
May 24th, 2010 at 3:35 pm
[...] 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 [...]
June 6th, 2010 at 9:59 am
[...] 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/ [...]
June 9th, 2010 at 7:11 pm
[...] http://jacwright.com/blog/70/how-to-listen-to-flash-events-that-dont-bubble/ [...]
July 3rd, 2010 at 2:46 pm
[...] 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/ [...]
November 5th, 2010 at 8:20 pm
[...] http://jacwright.com/blog/70/how-to-listen-to-flash-events-that-dont-bubble/ [...]
November 24th, 2010 at 1:44 am
[...] http://jacwright.com/blog/70/how-to-listen-to-flash-events-that-dont-bubble/ [...]
January 12th, 2011 at 2:02 am
[...] http://jacwright.com/blog/70/how-to-listen-to-flash-events-that-dont-bubble/ [...]
January 29th, 2011 at 2:41 am
[...] 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. [...]