我已经设置了自定义事件(见下文),但是当我在主类中监听事件并从子类调度它时,它永远不会被捕获。
尝试:
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));
}
}
}
答案 0 :(得分:3)
事件冒泡是你想要的:
Parent:
childClip.addEventListener('CUSTOM_EVENT', handler);
Child:
this.dispatchEvent(new Event('CUSTOM_EVENT', true, true));
这会将其传播到显示列表中。直接监听加载器的问题是它看起来像这样:
Loader
- Content
没有冒泡,你必须直接听内容,这是毫无意义的,因为在内容加载之前你不能听它。
答案 1 :(得分:0)
您并不需要将父项作为参数传递,特别是如果您打算在父项本身中侦听您的事件。您可以做的是将调度员作为参数传递,让调度员调度和调度。听听这个事件。
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')); } } }