Matt Williams
Matt Williams

Reputation: 79

Why isn't dispatchEvent firing?

okay so here is my problem in my main project I'm trying to fire an event using dispatchEvent I've made a simple test class to test this and yet it still isn't working...

Here is the test class

package 
{
import flash.display.Sprite;
import flash.events.Event;
public class Main extends Sprite 
{
    public function Main() {

        stage.addEventListener("pOver", rake);


        dispatchEvent(new Event("pOver"));
    }


    public function rake(e:Event):void {

        trace("working");
    }

}

Why isn't it firing? or why is the listener not capturing that event?

Upvotes: 0

Views: 3587

Answers (2)

J. Holmes
J. Holmes

Reputation: 18546

You are dispatching the event on the Main, which is a child of Stage. If you want to specifically dispatch an event on the Stage then use:

stage.dispatchEvent(new Event("pOver"));

Now you may be wondering, "If it's a child, then my event handler should still be getting triggered!"

Well, yes and no.

Lets take a look at a simple diagram of the event life-cycle:

enter image description here

First, the event that you are dispatching is not a bubbling Event. Examining the Event constructor, its signature looks like this:

public function Event(type:String, bubbles:Boolean = false, cancelable:Boolean = false)

Notice that the second argument is by default false, which means this event does not perform the bubbling part of the event life-cycle.

Second, you have attached the event dispatcher on the bubbling side of the event life-cycle. If you look at the signature for .addEventListener() it looks like this:

public function addEventListener(type:String, listener:Function, useCapture:Boolean = false, priority:int = 0, useWeakReference:Boolean = false):void

Notice the third argument. Which is by default false again. This means that you are attaching on the "bubbling" side of the event.

This means that this event is getting to the targeted element, the instance of Main, and then stopping and not going anywhere else.

TL;DR: So what does that all mean?

So to trigger your event handler, and not change where the event gets dispatched, you need to change your event that you are triggering to:

this.dispatchEvent(new Event("pOver", true));  // this event will bubble

Then your event handler, since it is a child, will be triggered by this event.

Conversely, I think that non-bubbling events will also progress through the capturing side of the event life-cycle so you could also change your event listener to attach to that side of the event as well.

stage.addEventListener("pOver", rake, true); // attach to a capturing side

I believe that event will always flow through the capturing phase, even if they are marked as not bubbling. But I could be wrong on that. I just can't remember if "non-bubbling" events skip both capturing and bubbling phases and just trigger the target event phase and I don't have time to check it right now.


Edit

So, I wrote up a quick test on wonderfl:

package {
    import flash.events.Event;
    import flash.display.Sprite;
    public class FlashTest extends Sprite {

        private var debug:TextField;

        public function FlashTest() {
            stage.addEventListener("Foo", bubbleFooHandler);
            stage.addEventListener("Foo", captureFooHandler, true);

            trace("Ready");

            trace("---------------");
            trace("Trying a non-bubbling event");
            this.dispatchEvent(new Event("Foo"));

            trace("---------------");
            trace("Trying a bubbling event");
            this.dispatchEvent(new Event("Foo", true));
        }

        private function captureFooHandler(e:Event):void {
            trace("Triggered \"Foo\" from capturing phase\n");
        }

        private function bubbleFooHandler(e:Event):void {
            trace("Triggered \"Foo\" from bubbling phase");
        }
    }
}

The output from this is

Ready
---------------
Trying a non-bubbling event
Triggered "Foo" from capturing phase
---------------
Trying a bubbling event
Triggered "Foo" from capturing phase
Triggered "Foo" from bubbling phase

Notice that events will always progress through the capturing phase. However, if they are not marked as a "bubbling event", see before, they will descent through the tree they stop when they arrive at target of the event (the EventDispatcher the event was dispatched on).

Bubbling events, on the other hand, will turn around and head back up the tree.

Hopefully, this clears things up.

Upvotes: 7

Mattias
Mattias

Reputation: 3907

First of all, you are listening to stage events. This means that as long as stage is not dispatching any Events you will not get any callbacks.

try

stage.dispatchEvent(new Event("pOver"));

Upvotes: 1

Related Questions