views:

47

answers:

2

I have got a custom event set up (see below), but when I listen for the event in my main class, and it gets dispatched from the child class, it never gets captured.

TRIED:

this.b.addEventHandler(GameLaunchEvent.GAME_LAUNCH_EVENT, this.eventHandler)


package com.thom.events 
{
    import flash.display.MovieClip;
    import flash.events.Event;

    /**
     * ...
     * @author 
     */
    public class LaunchEventAbstract extends Event
    {
        public var parent:MovieClip;
        public function LaunchEventAbstract(type:String, parent:MovieClip = null) 
        {
            super(type, true);  
            this.parent = parent;
        }   
    }
}

package com.thom.events 
{
    import flash.display.MovieClip;
    import flash.events.Event;

    /**
     * ...
     * @author
     */
    public class GameLaunchEvent extends LaunchEventAbstract 
    {
        public static const GAME_LAUNCH_EVENT:String = "GameLaunchEvent";
        public function GameLaunchEvent(parent:MovieClip = null) {
            trace("GameLaunchEvent");
            super(GAME_LAUNCH_EVENT, parent);
        }
    }

}

//example code
package {
   import com.thom.events.*;
   public class A extends MovieClip{
         public var b:B;
         public function A(){
              addEventListener(GameLaunchEvent.GAME_LAUNCH_EVENT, eventHandler);
              this.b = new B();
              addChild(b);
         }
         public function eventHandler(e:GameLaunchEvent){
             trace("Success");
         }
    }
}
package {
    import com.thom.events.*;
    public class B extends MovieClip{
         public function B() {
              dispatchEvent(new GameLaunchEvent(this));
         }
     }
}
+1  A: 

You don't really need to pass the parent as a parameter , particularly if you intend to listen to your event in the parent itself. What you can do is pass a dispatcher as a parameter and let the dispatcher both dispatch & listen to the event.


package 
{
   import flash.display.MovieClip;
   import flash.events.Event;
   import flash.events.EventDispatcher;

   public class A extends MovieClip
   {
         public var b:B;
         private var _dispatcher:EventDispatcher = new EventDispatcher();

         public function A()
         {
         _dispatcher.addEventListener('test', eventHandler);   
             this.b = new B(_dispatcher);
         }

         public function eventHandler(e:Event):void
         {
             trace("Success");
         }
    }
}


package 
{
    import flash.display.MovieClip;
    import flash.events.Event;
    import flash.events.EventDispatcher;

    public class B extends MovieClip
    {
        private var _dispatcher:EventDispatcher;

        public function B(dispatcher:EventDispatcher) 
        {
            this._dispatcher = dispatcher;
            _dispatcher.dispatchEvent(new Event('test'));
        }

    }
}
PatrickS
Sorry for the late responses. The parent is needed to destroy it later on, but that is not relevant. Seems like a way to reach my goals.
The Guy Of Doom
+1  A: 

Event Bubbling is what you want:

Parent:
childClip.addEventListener('CUSTOM_EVENT', handler);

Child:
this.dispatchEvent(new Event('CUSTOM_EVENT', true, true));

This will propagate it up the display list. The problem with listening to a loader directly is that it looks like this:

Loader
  - Content

Without bubbling you'd have to listen to the content directly, which is kind of pointless since you can't listen to it until the content has been loaded.

Daniel Hai
Strangely enough, this works with one of 2 classes. The other does not :(. But I sense that has to do with it not being loaded completly yet. +1 anyways :P
The Guy Of Doom
Make sure that in your childClip to do a dispatch the event ONLY after it's been added to the stage: so. addEventListener(Event.ADDED_TO_STAGE, dispatchInit)
Daniel Hai