- 使用数据参数创建自定义事件
- 您的类((例如 MyObject)应该使用 addEventListener 捕获 Event.COMPLETE 并调度您的自定义事件 MyCustomEvent,其中包含您的数据
- 在其他地方捕获您的 MyCustomEvent(例如在 MyAnotherObject 中)
MyCustomEvent.as
package
{
import flash.events.Event;
public class MyCustomEvent extends Event
{
public static const MY_CUSTOM_EVENT_COMPLETE:String = "myCustomEventComplete";
public var data:Object;
// Constructor
public function CaretEvent(type:String, data:Object, bubbles:Boolean=false, cancelable:Boolean=false)
{
super(type, true, cancelable);
this.data = data;
}
// Public Methods
public override function clone():MyCustomEvent
{
return new MyCustomEvent(type, data, bubbles, cancelable);
}
}
}
我的对象.as
package
{
import flash.events.EventDispatcher;
import flash.events.Event;
public class MyObject extends EventDispatcher
{
// Contructor
public function MyObject()
{
addEventListener(Event.COMPLETE, onComplete)
}
// Private Methods
private function onComplete(event:Event):void
{
dispatchEvent(new MyCustomEvent(MyCustomEvent.MY_CUSTOM_EVENT_COMPLETE, "This is my custom data"));
}
}
}
我的另一个对象.as
package
{
import flash.events.Event;
import flash.events.EventDispatcher;
public class MyAnotherObject extends EventDispatcher
{
// Constructor
public function MyAnotherObject()
{
addEventListener(MyCustomEvent.MY_CUSTOM_EVENT_COMPLETE, onMyCustomEventComplete)
}
// Private Methods
private function onMyCustomEventComplete(event:MyCustomEvent):void
{
trace(event.data);
}
}
}