代码之家  ›  专栏  ›  技术社区  ›  Bryan

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

  •  3
  • Bryan  · 技术社区  · 16 年前

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

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

    正在调度的被捏住的事件在类中嵌套了两个层次。我将类ZoomifyViewer的一个实例添加到舞台。ZoomifyViewer将ZoomGrid的实例添加到舞台,ZoomGrid将分派事件。

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

    调度员需要冒泡才能冒泡吗?

    另外,这两行的功能是否基于我的customvarevent中定义的常量picklicked相同?

     dispatchEvent(new CustomVarEvent(CustomVarEvent.pinClicked, hotspotData._name));
    
     dispatchEvent(new CustomVarEvent("pinClicked", hotspotData._name));
    
    4 回复  |  直到 7 年前
        1
  •  4
  •   Bryan Grezeszak    16 年前

    只有在以下情况下,事件才能在显示列表中冒泡 调度事件的对象是一个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));
        }
    }
    
        2
  •  3
  •   Stiggler    16 年前

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

        3
  •  0
  •   Jacob Poul Richardt    16 年前

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

    这正是您的问题,但如果您将分派类添加到显示列表中,我就看不到代码片段中的内容。

    另外,它看起来不像是定制的事件调度器,而是定制的事件。但我可能是错的(看不到你所有的代码)。

        4
  •  -1
  •   OXMO456    16 年前

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

    yourdispatcher.addEventListener(customvarevent.pinclicked,pinclickedHandler);

    在哪里? 你的调度员 是调度自定义事件的类。