How can I create multiple types & params for custom events?

ε祈祈猫儿з 提交于 2019-12-12 02:42:57

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!