我构建了一个传递变量的自定义事件调度程序。我分派了该事件,然后尝试在我的文档根目录中侦听该事件,但我从未收到该事件。如何将事件冒泡到我的文档类?
addEventListener(CustomVarEvent.pinClicked, pinClickedHandler);
function pinClickedHandler(e:CustomVarEvent) {
trace("main says " + e.arg[0] + " clicked");//access arguments array
}
package zoomify.viewer
{
import com.maps.CustomVarEvent;
protected function hotspotClickHandler(event:MouseEvent):void {
var hotspotData:Hotspot = hotspotsMap[event.currentTarget] as Hotspot;
trace(hotspotData._name + " was clicked");
/*if(hotspotData) {
navigateToURL(new URLRequest(hotspotData.url), hotspotData.urlTarget);
}*/
dispatchEvent(new CustomVarEvent("pinClicked",true,false,hotspotData._name));
}
}
package com.maps
{
// Import class
import flash.events.Event;
// CustomVarEvent
public class CustomVarEvent extends Event {
public static const pinClicked:String = "pinClicked";
// Properties
public var arg:*;
// Constructor
public function CustomVarEvent(type:String, ... a:*) {
var bubbles:Boolean = true;
var cancelable:Boolean = false;
super(type, bubbles, cancelable);
arg = a;
}
// Override clone
override public function clone():Event{
return new CustomVarEvent(type, arg);
};
}
}
正在调度的 pinClicked 事件嵌套在类深处的两层。我将 ZoomifyViewer 类的实例添加到舞台。ZoomifyViewer 将 ZoomGrid 实例添加到舞台,并且 ZoomGrid 调度事件。
当我将相同的事件侦听器和处理程序函数直接添加到我的 ZoomGrid 类(与分派事件相同的类)中时,侦听器和处理程序正常工作。但是,当侦听器和处理程序在父类中或在舞台上时,我没有得到任何响应。
调度员是否需要冒泡才能冒泡?
此外,根据我的 CustomVarEvent 中定义的常量 pinClicked,这两行在功能上是否相同?
dispatchEvent(new CustomVarEvent(CustomVarEvent.pinClicked, hotspotData._name));
dispatchEvent(new CustomVarEvent("pinClicked", hotspotData._name));