dispatchEvent

Roger Braunstein + Mims Wright = this blog


A reader sent me this question. Thanks in advance to Michael:

i’m dispatching an event from a sprite after it fades in completely
dispatchEvent(new Event(Main.ENTER_SCREEN_COMPLETE));

i’m trying to listen for to this event but for some reason it doesn’t get picked up when the capturing parameter is set to false.. that is for bubbling and the target phase.

This works
container.addEventListener(Main.ENTER_SCREEN_COMPLETE, screenTweenComplete, true, 0, true);

This does NOT work
container.addEventListener(Main.ENTER_SCREEN_COMPLETE, screenTweenComplete, false, 0, true);

Where container, is an ancestor to everything on the screen. Essentially its the the stage.

Any clues why this is happening?

Answer after the jump.

Hey Michael,

From the docs I see that:

useCapture:Boolean (default = false) — Determines whether the listener works in the capture phase or the target and bubbling phases. If useCapture is set to true, the listener processes the event only during the capture phase and not in the target or bubbling phase. If useCapture is false, the listener processes the event only during the target or bubbling phase. To listen for the event in all three phases, call addEventListener twice, once with the useCapture set to true, then again with useCapture set to false.

which tells me that you’re only going to get the capture phase of the event when its set to true and not the bubble or target phases. If the event is dispatched from a child of container, that means you won’t ever get the target phase in the container (since it’s not the target) and you will only get the bubble phase if the Event has bubbles=true;

bubbles:Boolean (default = false) — Determines whether the Event object participates in the bubbling stage of the event flow. The default value is false.

Most events are set to bubble = false by the system unless they are triggered by the API and are related to the display list. I see that in your example, you’re not setting bubble to true which means it won’t use the optional bubble phase.
So, in short, if you use
dispatchEvent(new Event(Main.ENTER_SCREEN_COMPLETE, true));
either one should work.

No Comments yet »

RSS feed for comments on this post. TrackBack URI

Leave a comment

XHTML: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <code> <em> <i> <strike> <strong>