How do you bind a variable to a function in as3

后端 未结 5 1477
孤独总比滥情好
孤独总比滥情好 2020-12-21 16:08

I have this code:

for each(var tool in tools){
tool.addEventListener(MouseEvent.MOUSE_DOWN, function(){
    trace(tool); //Always the last tool  
 });
}
         


        
5条回答
  •  梦毁少年i
    2020-12-21 16:56

    Try this.

    for each(var tool in tools){
      tool.addEventListener(MouseEvent.MOUSE_DOWN, toolFunction )
    }
    
    function toolFunction (e:MouseEvent){
      trace(e.currentTarget)
    }
    

    Aftear reading question title again, i realized, that what u need is custom event or:

    for each(var tool in tools){
          tool.addEventListener(MouseEvent.CLICK,
                  function(e:MouseEvent){toolFunction (e, "another param")},
                  false, 0, true);
        }
    
        function toolFunction (e:MouseEvent,anotherParam:String){
          trace(e.currentTarget)
          trace(anotherParam) //output "another param"
        }
    

提交回复
热议问题