从引发Event.COMPLETE如何获得相关的URLRequest通过的URLLoader发射如何获得、COMPLETE、Event、URLRequest

2023-09-09 21:46:17 作者:渴死的鱼⊙▽⊙

因此​​,让我们说,我们要加载一些XML -

  VAR XMLURL:字符串='的content.xml;
VAR xmlURLRequest:的URLRequest =新的URLRequest(XMLURL);
VAR xmlURLLoader:的URLLoader =新的URLLoader(xmlURLRequest);
xmlURLLoader.addEventListener(引发Event.COMPLETE,功能(五:事件):无效{
 跟踪('装',XMLURL);
 跟踪(XML(e.target.data));
});
 

如果我们需要知道那个特定的XML文档的源URL,我们有这个变量告诉我们,对不对?现在让我们想象一下,XMLURL变量不在身边,帮助我们 - 也许我们要加载3 XML文档,按顺序命名的,而且我们要使用一次性的变量在for循环:

 的(VAR我:UINT = 3; I> 0; I  - ){
 VAR xmlURLLoader:的URLLoader =新的URLLoader(新的URLRequest(内容+ I +)XML。');
 xmlURLLoader.addEventListener(引发Event.COMPLETE,功能(五:事件):无效{
  跟踪(e.target.src); //我希望这个工作...
  跟踪(XML(e.target.data));
 });
}
 

突然,它不是那么容易吧?

我恨你不能只说e.target.src或什么 - 是那里URLLoaders与他们从加载数据的URL相关联的好办法?我失去了一些东西?这种感觉不是很直观给我。

解决方案

 的(VAR我:UINT = 3; I> 0;我 - ){
    变种SRC:的URLRequest =新的URLRequest(内容+ I +XML。');
    VAR xmlURLLoader:的URLLoader =新的URLLoader(SRC);
    xmlURLLoader.addEventListener(引发Event.COMPLETE,函数(要求:的URLRequest):功能{
        复位功能(五:事件):无效{
            跟踪(REQ); //应工作
            //无论你需要做的
        }
    }(SRC));
}
 
绿色先锋下载2017年11月14日绿色精品更新

你得使用的第二个功能包的要求,否则,所有的三个事件侦听器将引用的最后一个请求。

So let's say we want to load some XML -

var xmlURL:String = 'content.xml';
var xmlURLRequest:URLRequest = new URLRequest(xmlURL);
var xmlURLLoader:URLLoader = new URLLoader(xmlURLRequest);
xmlURLLoader.addEventListener(Event.COMPLETE, function(e:Event):void{
 trace('loaded',xmlURL);
 trace(XML(e.target.data));
});

If we need to know the source URL for that particular XML doc, we've got that variable to tell us, right? Now let's imagine that the xmlURL variable isn't around to help us - maybe we want to load 3 XML docs, named in sequence, and we want to use throwaway variables inside of a for-loop:

for(var i:uint = 3; i > 0; i--){
 var xmlURLLoader:URLLoader = new URLLoader(new URLRequest('content'+i+'.xml'));
 xmlURLLoader.addEventListener(Event.COMPLETE, function(e:Event):void{
  trace(e.target.src); // I wish this worked...
  trace(XML(e.target.data));
 });
}

Suddenly it's not so easy, right?

I hate that you can't just say e.target.src or whatever - is there a good way to associate URLLoaders with the URL they loaded data from? Am I missing something? It feels unintuitive to me.

解决方案

for (var i:uint = 3; i > 0; i--) {
    var src:URLRequest = new URLRequest('content'+i+'.xml');
    var xmlURLLoader:URLLoader = new URLLoader(src);
    xmlURLLoader.addEventListener(Event.COMPLETE, function(req:URLRequest):Function {
        return function(e:Event):void {
            trace(req); // Should work
            // whatever you need to do
        }
    }(src));
}

You've gotta use the second function to wrap the request, otherwise all three event listeners will reference the last request.