Reputation: 7034
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));
}
}
}
Upvotes: 0
Views: 2380
Reputation: 2571
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.
Upvotes: 3
Reputation: 9572
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')); } } }
Upvotes: 0