inf actionscript 3(as3),如何在匿名函数中传递参数?

svi*_*irk 1 apache-flex actionscript-3

在as3中,我添加了事件监听器,然后将匿名函数附加到它:

myBox.addEventListener(MouseEvent.ROLL_OVER,function(e:MouseEvent):void {Alert.show(count,'Alert Box'););

现在这整段代码循环了n次.现在,我有了我的盒子,每当我将鼠标放在盒子上时,它应该提醒你的名字.但是,我看到的是每个盒子使用count的最后一个值.

如何将参数或值传递给匿名函数?(作为翻转,我相信,只需要一个变量)

Ric*_*lay 5

您需要通过执行函数来创建新范围:

for (var i:int = 0; i<10; i++)
{
    (function(count:int):void
    {
        myBox.addEventListener(MouseEvent.ROLL_OVER, 
            function(e:MouseEvent):void { Alert.show(count, 'Alert Box'); });
    })(i);
}
Run Code Online (Sandbox Code Playgroud)