本文介绍了如何使用添加的数据调度事件 - AS3的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
谁能给我一个简单的例子,说明如何在 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));
这里的 result
是我想与事件一起传递的对象.
Here result
is an object that I want to pass along with the event.
推荐答案
如果你想通过一个事件传递一个对象,你应该创建一个自定义事件.代码应该是这样的.
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);
}
}
然后你可以像这样使用上面的代码:
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.
}
这篇关于如何使用添加的数据调度事件 - AS3的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!