AS3:通过dispatchEvent- pass params调度事件?

jml*_*jml 0 apache-flex events actionscript-3

我想从班上发一个活动和一个网址.

我知道我可以做以下事情:

import flash.events.EventDispatcher;

private function thumbClick(e:MouseEvent):void
{
   dispatchEvent(new Event("clicked"));
}
Run Code Online (Sandbox Code Playgroud)

但是我不知道如何将params与事件一起发送......?

另外,在我的主要app跑步者中,我尝试:

var my_ev:Event = new Event("clickedImage");
my_ev.hasOwnProperty(e.currentTarget.link);
dispatchEvent(my_ev);
Run Code Online (Sandbox Code Playgroud)

...但我不确定这是正确的语法.

谢谢你的帮助,jml

Joe*_*oks 5

艾伦是对的,你会想做一个自定义的事件.有几点需要注意:

import flash.events.Event;  

public class ThumbnailEvent extends Event
{
    public static var THUMB_CLICKED:String = "thumbClicked";

    private var _url:String;
    public function get url():String { return _url }

    public function ThumbnailEvent (type:String, url:String, bubbles:Boolean=false, cancelable:Boolean=false)
    {
        super(type,bubbles,cancelable);
        _url = url
    }

    override public function clone():Event
    {
        return new ThumbnailEvent(type, url, bubbles, cancelable);
    }
}
Run Code Online (Sandbox Code Playgroud)

自定义事件需要始终覆盖克隆.如果事件冒泡或以任何方式中继,则需要此方法.自定义属性应该是私有的,只有一个只读的getter.这是一个标准惯例,以防止在整个事件的生命周期中改变属性.

使用此方法会将您的代码更改为:

private function thumbClick(e:MouseEvent):void
{
   dispatchEvent(new ThumbnailEvent(ThumbnailEvent.THUMB_CLICKED, myUrlString));
}

//elsewhere

addEventListener(ThumbnailEvent.THUMB_CLICKED, thumbClickedHandler);

private function thumbClickedHandler(event:ThumbnailEvent):void
{
    var link:String = event.url;
}
Run Code Online (Sandbox Code Playgroud)

adobe.com上的自定义事件教程