如何调度事件有增加的数据 - AS3事件、数据

2023-09-08 12:13:17 作者:゛mm、绅绅绅绅、绅士

任何一个可以给我如何在分发事件中的ActionScript3与连接到它,就像

Can any one give me a simple example on how to dispatch an event in actionscript3 with an object attached to it, like

dispatchEvent( new Event(GOT_RESULT,result));

下面结果是我想通过与事件一起的对象。

Here result is an object that I want to pass along with the event.

推荐答案

如果你想通过你应该创建一个自定义事件的事件传递对象。在code应该是这样的。

In case you want to pass an object through an event you should create a custom event. The code should be something like this.

public class MyEvent extends Event
{
    public static const GOT_RESULT:String = "gotResult";

    // this is the object you want to pass through your event.
    public var result:Object;

    public function MyEvent(type:String, result:Object, bubbles:Boolean=false, cancelable:Boolean=false)
    {
        super(type, bubbles, cancelable);
        this.result = result;
    }

    // always create a clone() method for events in case you want to redispatch them.
    public override function clone():Event
    {
        return new MyEvent(type, result, bubbles, cancelable);
    }
}

然后就可以使用上面这样的code:

Then you can use the code above like this:

dispatchEvent(new MyEvent(MyEvent.GOT_RESULT, result));

和你听这个事件在必要。

And you listen for this event where necessary.

addEventListener(MyEvent.GOT_RESULT, myEventHandler);
// more code to follow here...
protected function myEventHandler(event:MyEvent):void
{
    var myResult:Object = event.result; // this is how you use the event's property.
}