変数を渡すカスタム イベント ディスパッチャーを作成しました。イベントをディスパッチしてから、ドキュメント ルートでイベントをリッスンしようとしましたが、イベントを受け取りません。イベントをドキュメント クラスにバブルアップするにはどうすればよいですか?
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 イベントは、クラス内で 2 レベルの深さでネストされています。クラス ZoomifyViewer のインスタンスをステージに追加します。ZoomifyViewer は ZoomGrid のインスタンスをステージに追加し、ZoomGrid はイベントをディスパッチします。
同じイベント リスナーとハンドラー関数を ZoomGrid クラス (イベントのディスパッチ元と同じクラス) に直接追加すると、リスナーとハンドラーは適切に機能します。ただし、リスナーとハンドラーが親クラスまたはステージ上にある場合、応答がありません。
バブルアップするにはディスパッチャが必要ですか?
また、これら 2 つの行は、CustomVarEvent で定義されている定数 pinClicked に基づいて機能的に同一ですか?
dispatchEvent(new CustomVarEvent(CustomVarEvent.pinClicked, hotspotData._name));
dispatchEvent(new CustomVarEvent("pinClicked", hotspotData._name));