如何从 ActionScript 3 类分派自定义事件并在文档根目录中侦听它?

发布于 2024-07-17 06:13:38 字数 1845 浏览 6 评论 0原文

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

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

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.

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.

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?

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

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

一江春梦 2024-07-24 06:13:38

仅当调度事件的对象是 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));
    }
}

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.

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));
    }
}
┾廆蒐ゝ 2024-07-24 06:13:38

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

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?

装迷糊 2024-07-24 06:13:38

仅当调度事件的对象位于显示列表上时,冒泡才起作用。 这是舞台的任何孙子。

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

另外,它看起来不像您创建了自定义 EventDispatcher,而是自定义了 Event。 但我可能是错的(看不到你的所有代码)。

Bubbling only works when the object that dispatches the event is on the displaylist. Which is any grandchild of the stage.

That is properly your problem, but I can't see from code snippet if you add you dispatching class to the 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).

一个人的夜不怕黑 2024-07-24 06:13:38

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

yourDispatcher.addEventListener(CustomVarEvent.pinClicked, pinClickedHandler);

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

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文