3

我构建了一个传递变量的自定义事件调度程序。我分派了该事件,然后尝试在我的文档根目录中侦听该事件,但我从未收到该事件。如何将事件冒泡到我的文档类?

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));
4

4 回答 4

4

如果调度事件的对象是 DisplayObject (或 DisplayObject 的祖先,例如 Sprite 或 MovieClip),则事件只能在显示列表中冒泡,以便它可以在显示列表中并添加到显示中事件调度时的列表

您编写的代码根本没有将事件作为类的一部分分派的行,只是包中的一个函数,所以我不确定您打算将它冒泡到哪里。

一个快速的解决方法是让被点击的同一个对象触发事件,因为它被点击了,它显然是一个在舞台上的显示对象,这符合我们关于冒泡的两个规定。


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));
    }
}
于 2009-04-15T17:46:38.823 回答
3

事件在显示列表中冒泡。我无法从您的代码示例中得知您从哪个对象分派事件,只能知道您在类上有一个方法可以这样做。该类的实例是否已添加到显示列表中?

于 2009-04-14T23:39:26.490 回答
0

仅当分派事件的对象在显示列表中时,冒泡才有效。这是舞台的任何孙子。

这确实是您的问题,但是如果您将调度类添加到显示列表中,我无法从代码片段中看到。

此外,看起来您制作的不是自定义 EventDispatcher,而是自定义事件。但我可能是错的(看不到你的所有代码)。

于 2009-04-15T00:12:30.277 回答
-1

为了侦听您的自定义事件,您应该在文档类中对调度程序有一个有效的引用。

yourDispatcher.addEventListener(CustomVarEvent.pinClicked, pinClickedHandler);

其中yourDispatcher是调度自定义事件的类。

于 2009-04-15T08:27:04.503 回答