开发者

Action Script: When binding event listeners in an automated way, only the last one from the list becomes functional..?

开发者 https://www.devze.com 2023-02-05 05:47 出处:网络
Here is piece of code: protected function handleTriggers(raw:Object) : void { var name:String, value:String, map:Object;

Here is piece of code:

protected function handleTriggers(raw:Object) : void
{
    var name:String, value:String, map:Object;

    map = {
            'onclick':      MouseEvent.CLICK,
            'ondblclick':   MouseEvent.DOUBLE_CLICK,
            'onmousedown':  MouseEvent.MOUSE_DOWN,
            'onmouseup':    MouseEvent.MOUSE_UP,
            'onmouseleave': MouseEvent.ROLL_OUT,
            'onrollout':    MouseEvent.ROLL_OUT,
            'onmouseenter': MouseEvent.ROLL_OVER, 
            'onrollover':   MouseEvent.ROLL_OVER,
            'onmouseover':  MouseEvent.MOUSE_OVER,
            'onmouseout':   MouseEvent.MOUSE_OUT,
            'onmousemove':  MouseEvent.MOUSE_MOVE
    };

    for (name in raw) 
    {
        value = raw[name];
        if (name in map) {
            var cloneValue:String = value;
            object.addEventListener(map[name], function(event:* = null) : void {
                execute(cloneValue, event); 
     开发者_C百科       });
        }
    }
}

object is generic DisplayObject and raw object contains series of onclick, onmouseover, etc event triggers. It was meant that I could attach event listeners automatically following the properties of that raw object. And it seems like it works, as I indeed can see some listener Functions in listeners property of the DisplayObject and their number corresponds to the number of triggers. But later when everything is put on stage, only the last event handler gets triggered.

Anyone any idea why that could be happening?


Your problem is similar to this one. Because you add an inline function as eventlistener the clonevalue has always the value of the last value in raw.

I think this should work (untested):

object.addEventListener(map[name], function(cloneValue : String) : Function {
    return function(event:* = null) : void {
         execute(cloneValue, event);
    };
})(cloneValue);
0

精彩评论

暂无评论...
验证码 换一张
取 消