0

我需要创建一个事件函数,其中包含您传入的参数,看起来像这样。

    public function foo(e:Event, args)
    {
        trace(args[0]);
    }


    addEventListener("fooEvent", foo);
    dispatchEvent(new Event("fooEvent", true), args);

无论如何,我可以做到这一点?多谢你们。

4

1 回答 1

0

尝试这个:

package {
  import flash.events.Event;

  public class CustomEvent extends Event {
    public var params:Object;

    public function CustomEvent(type:String, paramsObj:Object = null, bubbles:Boolean = true, cancelable:Boolean = true) {
      super(type, bubbles, cancelable);

      if (paramsObj) {
        params = paramsObj;
      }
    }

    public override function clone():Event{
      return new CustomEvent(type, params, bubbles, cancelable);
    }

    public override function toString():String{
      return formatToString("CustomEvent", "params", "type", "bubbles", "cancelable");
    }
  }
}

然后,您可以像这样构造事件:

dispatchEvent(new CustomEvent("fooEvent", {param1:"foo", param2:"bar"}))

并检索数据,如:

addEventListener("fooEvent", function(event:CustomEvent):void {
  trace("param1: " + event.params.param1);
  trace("param2: " + event.params.param2);
});
于 2013-05-30T06:26:51.880 回答