在 ActionScript 3 中,如何将循环中数组的当前值传递给事件侦听器 [英] In ActionScript 3, how can I pass the current value of an array in a loop to an event listener
本文介绍了在 ActionScript 3 中,如何将循环中数组的当前值传递给事件侦听器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
代码示例:
var gospels : Array = ["john", "mark", "matthew", "paul"];
for each (var book : String in gospels)
{
var loader : URLLoader = new URLLoader();
loader.load(new URLRequest("http://example.com/" + name));
trace(book) // outputs current value of array
loader.addEventListener(Event.COMPLETE, function(e : Event) : void {
trace(book); // prints paul 4 times
});
}
当事件侦听器的函数被调用时,如何让事件侦听器在循环中使用数组的值?IE.当我在事件侦听器的函数中调用 trace 时,如何让它输出 "john"
, "mark"
, "matthew"
,和 "paul"
?
How can I get the event listener to use the value of the array in the loop when the event listener's function was called? I.e. when I call trace inside the event listener's function, how can I get it to output "john"
, "mark"
, "matthew"
, and "paul"
?
推荐答案
var gospels:Array = ["john", "mark", "matthew", "paul"];
for each (var item:String in gospels)
{
(function(book:String){
var loader : URLLoader = new URLLoader();
loader.load(new URLRequest("http://example.com/" + name));
trace(book) // outputs current value of array
loader.addEventListener(Event.COMPLETE, function(e:Event):void {
trace(book); // prints paul 4 times
});
}(item));
}
这篇关于在 ActionScript 3 中,如何将循环中数组的当前值传递给事件侦听器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文