To pass a parameter to event listener in AS3 the simple way… does it exist?

前端 未结 5 1775
轻奢々
轻奢々 2020-11-28 04:00

Expected / pseudo example:

stage.addEventListener(MouseEvent.CLICK, onClick.someWayToPassParameters(true, 123, 4.56, "string"));
function onClick(e:         


        
5条回答
  •  一生所求
    2020-11-28 04:25

    I would modify Creynders example slightly:

        protected function createHandler(handler:Function, ...args):Function
        {
            var h:Function = function(e:Event = null):void
            {
                trace(args);
    
                if (handler != null)
                    handler(e);
            }
            return h;
        }
    
        sm.addEventListener(StyleEvent.STYLE_CHANGE, 
                createHandler(updateStyle, 1,true,"Lorem",["a","b","c"]), 
                false, 0, true);
    

    this way you can use original event handlers and inject the "arguments" to it,

    also you could have your handlers to have following signature:

    protected function myHandler(e:Event, ...args):void;
    

    then you could pass arguments directly to the target handler:

        protected function createHandler(handler:Function, ...args):Function
        {
            var h:Function = function(e:Event = null):void
            {
                //trace(args);
    
                if (handler != null)
                {
                    args.unshift(e);
                    handler.apply(this, args);
                }
            }
            return h;
        }
    

    best regards

提交回复
热议问题