2009-04-14 23 views
3

我构建了一个传递变量的自定义事件调度程序。我派遣活​​动,然后尝试在文档根目录中听取活动,但我从未收到活动。我如何将活动冒泡到我的文档类?如何从ActionScript 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

事件只能冒泡通过显示列表中,如果分派该事件是一个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)); 
    } 
} 
3

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

0

冒泡只有在调度事件的对象位于显示列表上时才起作用。这是舞台的孙子。

这是正确的问题,但如果您添加了将类分派给显示列表,则无法从代码段看到。

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

+0

对不起,但是这个答案除了我提供的那个副本外什么都没有? – Stiggler 2009-04-15 18:25:08

-1

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

yourDispatcher.addEventListener(CustomVarEvent.pinClicked,pinClickedHandler);

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

+0

他试图让事件冒起来... – 2009-04-15 09:17:03

+0

是的。事实上它非常简单。我用它所有的时间。这是继承的最大优点之一,您可以添加功能而不会丢失任何内容。 – 2009-04-15 10:18:59

相关问题