Actionscript 3 AS3 dispatchEvent不工作
我搜索了其他发送事件的示例,但没有一个对我有帮助 这是我到目前为止所拥有的。我知道事件侦听器已成功添加,但事件从未被调度 在.mxml文件中:Actionscript 3 AS3 dispatchEvent不工作,actionscript-3,apache-flex,event-handling,dispatchevent,event-listener,Actionscript 3,Apache Flex,Event Handling,Dispatchevent,Event Listener,我搜索了其他发送事件的示例,但没有一个对我有帮助 这是我到目前为止所拥有的。我知道事件侦听器已成功添加,但事件从未被调度 在.mxml文件中: function foo():void { var eventClassObj:MyEventClass = new MyEventClass(); } 在单独的.as文件中: public class MyEventClass extends EventDispatcher { public function MyEvent
function foo():void {
var eventClassObj:MyEventClass = new MyEventClass();
}
在单独的.as文件中:
public class MyEventClass extends EventDispatcher
{
public function MyEventClass(target:flash.events.IEventDispatcher = null)
{
//ADD EVENT LISTENER
this.addEventListener("test", testFunc, true);
//DEBUGGING PRINT STATEMENTS
var str:String;
if (this.hasEventListener("test")) {
str = "EVENT LISTENER ADDED";
} else {
str = "NO LISTENER";
}
ExternalInterface.call("console.log", str);
//DISPATCH EVENT
this.dispatchEvent(new Event("test", true));
}
//THIS MUST EXECUTE WHEN EVENT DISPATCHED
private function testFunc(e:Event):void {
ExternalInterface.call("console.log", "dispatch event successful");
}
}
我对ActionScript3一无所知。您知道为什么事件从未被发送吗?我在控制台中运行此命令的输出仅为:
EVENT LISTENER ADDED
而我的预期产出是:
EVENT LISTENER ADDED
dispatch event successful
我不明白这为什么行不通 问题源于添加事件侦听器的方式。您将
useCapture
设置为true
,这样的设置可以防止侦听器对冒泡事件做出反应
如果更改以下行:
this.addEventListener("test", testFunc, true);
致:
事件将由您的
testFunc
处理。这已经有一段时间了,但我猜您的事件侦听器没有被触发。b/c发送事件的内容不在闪存显示列表中。根据定义,MXML对象是事件分派器。因此,您不需要单独的EventDispatcher
——您甚至可以从MXML类中侦听并分派“测试”。如果您确实想让另一个类侦听事件,则需要将其添加到显示列表中(使其成为UIComponent
或w/special care,您可以使用Sprite
)。
this.addEventListener("test", testFunc);