I built a custom event dispatcher that passes variables. I dispatch the event and then I try to listen for the event in my document root, but I never receive the event. How do I bubble the event up to my document class?
我构建了一个传递变量的自定义事件调度程序。我发送事件,然后我尝试在我的文档根目录中监听事件,但我从未收到过该事件。如何将事件冒泡到我的文档类?
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);
};
}
}
The pinClicked event that is being dispatched is nested two levels deep in classes. I add an instance of class ZoomifyViewer to the stage. ZoomifyViewer adds and instance of ZoomGrid to the stage and ZoomGrid dispatches the event.
正在调度的pinClicked事件嵌套在类的两个深层。我在舞台上添加了一个类ZoomifyViewer的实例。 ZoomifyViewer将ZoomGrid的实例添加到舞台,ZoomGrid将调度该事件。
When I add the same event listener and handler function directly into my ZoomGrid class (the same class that the event is dispatched from), then the listener and handler work properly. However, when the listener and handler are in a parent class or on the stage, I get no response.
当我将相同的事件监听器和处理函数直接添加到我的ZoomGrid类(调度事件的同一个类)时,监听器和处理程序正常工作。但是,当侦听器和处理程序位于父类或舞台上时,我没有得到任何响应。
Is a dispatcher necessary to bubble up to bubble up?
是否需要调度员冒泡起泡?
Also, are these two lines functionally identical based on the constant pinClicked that is defined in my CustomVarEvent?
另外,基于我的CustomVarEvent中定义的常量pinClicked,这两行在功能上是否相同?
dispatchEvent(new CustomVarEvent(CustomVarEvent.pinClicked, hotspotData._name));
dispatchEvent(new CustomVarEvent("pinClicked", hotspotData._name));
4 个解决方案
#1
The event can only bubble up through the display list if the object that dispatched the event is a DisplayObject (or an ancestor of DisplayObject, such as a Sprite or MovieClip) so that it can be in the display list AND it is added to the display list at the time of the event dispatch.
如果调度事件的对象是DisplayObject(或DisplayObject的祖先,例如Sprite或MovieClip),则事件只能通过显示列表冒泡,以便它可以在显示列表中并将其添加到显示中事件发送时的列表。
The code you have written does not have the line that dispatches an event as part of a class at all, just a function in a package, so I'm not sure where you intend on it bubbling to.
你编写的代码没有将一个事件作为一个类的一部分调度的行,只是一个包中的一个函数,所以我不知道你打算在哪里冒泡。
A quick fix for you would simply to have the same object that was clicked have the event dispatched off of it, cuz since it was clicked it is obviously a display object that is on stage, which meets our two stipulations for bubbling.
快速修复你只需要让被点击的同一个对象从它上面调出事件,因为它被点击它显然是一个在舞台上的显示对象,它符合我们对冒泡的两个规定。
package zoomify.viewer
{
import com.maps.CustomVarEvent;
protected function hotspotClickHandler(event:MouseEvent):void
{
// BY THE WAY, event.currentTarget will return the actual object, so you
// don't need whatever the hotspotsMap[] thing is, just cast event.currentTarget
/*var hotspotData:Hotspot = hotspotsMap[event.currentTarget] as Hotspot;*/
var hotspotData:Hotspot = event.currentTarget as Hotspot;
// here we dispatch the event off of the target, since it is definitely
// in the display list already, so therefore bubbling will work right
event.target.dispatchEvent(new CustomVarEvent("pinClicked",true,false,hotspotData._name));
}
}
#2
Events bubble up the display list. I cannot tell from your code sample what object you're dispatching the event from, only that you have a method on a class to do so. Has an instance of that class been added to the display list?
事件在显示列表中冒泡。我无法从您的代码示例中判断出您从哪个对象调度事件,只是您在类上有一个方法来执行此操作。该类的实例是否已添加到显示列表中?
#3
Bubbling only works when the object that dispatches the event is on the displaylist. Which is any grandchild of the stage.
仅当调度事件的对象位于displaylist上时,冒泡才有效。哪个是舞台的孙子。
That is properly your problem, but I can't see from code snippet if you add you dispatching class to the displaylist.
这是你的问题,但如果你将调度类添加到displaylist,我无法从代码片段中看到。
Also, it does not look like you made a custom EventDispatcher, but a custom Event. But I could be wrong (can't see all of your code).
此外,它看起来不像您自定义EventDispatcher,而是自定义事件。但我可能是错的(无法看到你的所有代码)。
#4
In order to listen your custom event you should have a valid reference to the dispatcher in your document class.
为了监听您的自定义事件,您应该对文档类中的调度程序有一个有效的引用。
yourDispatcher.addEventListener(CustomVarEvent.pinClicked, pinClickedHandler);
Where yourDispatcher is the class dispatching the custom event.
yourDispatcher是调度自定义事件的类。
#1
The event can only bubble up through the display list if the object that dispatched the event is a DisplayObject (or an ancestor of DisplayObject, such as a Sprite or MovieClip) so that it can be in the display list AND it is added to the display list at the time of the event dispatch.
如果调度事件的对象是DisplayObject(或DisplayObject的祖先,例如Sprite或MovieClip),则事件只能通过显示列表冒泡,以便它可以在显示列表中并将其添加到显示中事件发送时的列表。
The code you have written does not have the line that dispatches an event as part of a class at all, just a function in a package, so I'm not sure where you intend on it bubbling to.
你编写的代码没有将一个事件作为一个类的一部分调度的行,只是一个包中的一个函数,所以我不知道你打算在哪里冒泡。
A quick fix for you would simply to have the same object that was clicked have the event dispatched off of it, cuz since it was clicked it is obviously a display object that is on stage, which meets our two stipulations for bubbling.
快速修复你只需要让被点击的同一个对象从它上面调出事件,因为它被点击它显然是一个在舞台上的显示对象,它符合我们对冒泡的两个规定。
package zoomify.viewer
{
import com.maps.CustomVarEvent;
protected function hotspotClickHandler(event:MouseEvent):void
{
// BY THE WAY, event.currentTarget will return the actual object, so you
// don't need whatever the hotspotsMap[] thing is, just cast event.currentTarget
/*var hotspotData:Hotspot = hotspotsMap[event.currentTarget] as Hotspot;*/
var hotspotData:Hotspot = event.currentTarget as Hotspot;
// here we dispatch the event off of the target, since it is definitely
// in the display list already, so therefore bubbling will work right
event.target.dispatchEvent(new CustomVarEvent("pinClicked",true,false,hotspotData._name));
}
}
#2
Events bubble up the display list. I cannot tell from your code sample what object you're dispatching the event from, only that you have a method on a class to do so. Has an instance of that class been added to the display list?
事件在显示列表中冒泡。我无法从您的代码示例中判断出您从哪个对象调度事件,只是您在类上有一个方法来执行此操作。该类的实例是否已添加到显示列表中?
#3
Bubbling only works when the object that dispatches the event is on the displaylist. Which is any grandchild of the stage.
仅当调度事件的对象位于displaylist上时,冒泡才有效。哪个是舞台的孙子。
That is properly your problem, but I can't see from code snippet if you add you dispatching class to the displaylist.
这是你的问题,但如果你将调度类添加到displaylist,我无法从代码片段中看到。
Also, it does not look like you made a custom EventDispatcher, but a custom Event. But I could be wrong (can't see all of your code).
此外,它看起来不像您自定义EventDispatcher,而是自定义事件。但我可能是错的(无法看到你的所有代码)。
#4
In order to listen your custom event you should have a valid reference to the dispatcher in your document class.
为了监听您的自定义事件,您应该对文档类中的调度程序有一个有效的引用。
yourDispatcher.addEventListener(CustomVarEvent.pinClicked, pinClickedHandler);
Where yourDispatcher is the class dispatching the custom event.
yourDispatcher是调度自定义事件的类。