在ActionScript 3,我怎么能传递一个数组的当前值在循环中的事件监听器 [英] In ActionScript 3, how can I pass the current value of an array in a loop to an event listener

查看:107
本文介绍了在ActionScript 3,我怎么能传递一个数组的当前值在循环中的事件监听器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

code例:

  VAR福音:数组= [约翰,商标,马太福音,保];

每个(VAR书:字符串中的福音)
{
  VAR装载机:的URLLoader =新的URLLoader();
  loader.load(新的URLRequest(http://example.com/+名));

  跟踪(书)//阵列的输出电流值

  loader.addEventListener(引发Event.COMPLETE,功能(五:事件):无效{
    跟踪(书); //输出保罗4次
  });
}
 

我怎样才能得到事件侦听器来使用,在循环数组的值时,事件侦听器的函数调用?即当我打电话跟踪的事件侦听器的功能在里面,我怎么能得到它的输出约翰标记马太福音保罗

解决方案

  VAR福音:数组= [约翰,商标,马太福音,保];

每个(VAR项目:字符串中的福音)
{
  (函数(书:字符串){
    VAR装载机:的URLLoader =新的URLLoader();
    loader.load(新的URLRequest(http://example.com/+名));

    跟踪(书)//阵列的输出电流值

    loader.addEventListener(引发Event.COMPLETE,功能(五:事件):无效{
      跟踪(书); //输出保罗4次
    });
  }(项目));
}
 

Code Example:

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
  });
}

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屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆