问题
I have a custom event class and want to insert multiple parameters (like the native events: MOUSE_UP, CLICK, ROLL_OVER). Here's my code so far:
package
{
import flash.events.Event;
public class MenuGeneratorEvent extends Event
{
public static const PASS_PARAMS:String = "passParams";
public var param1:String = new String();
public function MenuGeneratorEvent(type:String, str:*, bubbles:Boolean = false,
cancelable:Boolean = false):void
{
this.param1 = str;
super(type, bubbles, cancelable);
}
override public function clone():Event
{
return new MenuGeneratorEvent(type, param1, bubbles, cancelable);
}
}
}
I'm using Flash Professional CC 2015. Is it possible to do this?
回答1:
Just add them as construcor arguments and make it visible outside just like you did with param1
.
If you have to many parameters to pass as constructor arguments you can always wrap them inside another object. An if you want have access to them directly from event object just create accessors to them:
public class MenuGeneratorEvent extends Event
{
public static const PASS_PARAMS:String = "passParams";
private var params:Object;
public function MenuGeneratorEvent(type:String, params:Object, bubbles:Boolean = false,
cancelable:Boolean = false):void
{
this.params = params;
super(type, bubbles, cancelable);
}
override public function clone():Event
{
return new MenuGeneratorEvent(type, params, bubbles, cancelable);
}
public function get param1():String{return params.param1;}
public function get param2():Number{return params.param2;}
public function get param3():Object{return params.param3;}
}
来源:https://stackoverflow.com/questions/32126410/how-can-i-create-multiple-types-params-for-custom-events