How do you bind a variable to a function in as3

后端 未结 5 1486
孤独总比滥情好
孤独总比滥情好 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条回答
  •  孤城傲影
    2020-12-21 17:06

    You can't nest functions inside loops the parameters for the function call will have the same value across all iterations of the loop, and that value will be from the last most iteration. There are some hacks to handle this, but it is not good coding practice and makes it hard in the future to modify it.

    What you need to do is more OOP style.

    Since the Tool class is obviously custom you need to modify it to hold the values or whatever references you were talking about for the future.
    Basically, if you have a value you need to pass along that is related to that object then just make that value an attribute of the class Tool.

提交回复
热议问题