actionscript-3 - 如何从 actionscript 3 类调度自定义事件并在文档根目录中监听它?

标签 actionscript-3 events dispatcher

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

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


}

正在分派(dispatch)的 pinClicked 事件在类中嵌套了两层。我将类 ZoomifyViewer 的一个实例添加到舞台上。 ZoomifyViewer 将 ZoomGrid 的实例添加到舞台,ZoomGrid 调度该事件。

当我将相同的事件监听器和处理程序函数直接添加到我的 ZoomGrid 类(从中调度事件的同一类)时,监听器和处理程序将正常工作。但是,当监听器和处理程序在父类中或在舞台上时,我没有得到任何响应。

调度员是否需要冒泡来冒泡?

此外,根据我的 CustomVarEvent 中定义的常量 pinClicked,这两行在功能上是否相同?

 dispatchEvent(new CustomVarEvent(CustomVarEvent.pinClicked, hotspotData._name));

 dispatchEvent(new CustomVarEvent("pinClicked", hotspotData._name));

最佳答案

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

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

一个快速修复方法是简单地让被点击的同一个对象从它那里分派(dispatch)事件,因为既然它被点击了,它显然是一个在舞台上的显示对象,这符合我们关于冒泡的两个规定。


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

关于actionscript-3 - 如何从 actionscript 3 类调度自定义事件并在文档根目录中监听它?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/749546/

相关文章:

iphone - 如何捕获自定义uitableViewCell中的开关更改事件?

C++ 事件处理

c# - 为什么当我传递参数时调度程序会这样?

apache-flex - 弹性/ ActionScript : Dynamically accessing static var

flash - 我如何在 flex 中以 MX 数据网格列的百分比设置宽度?

actionscript-3 - 当我添加按钮时,为什么动态文本消失了?

c# - WPF Dispatcher 线程卡住主窗口

algorithm - 从数字数组中获取最小值或最大值的最佳方法是什么?

java - 如何在 Composite GWT 中实现 SelectionEvent

php - symfony2事件调度程序延迟加载监听器