URLLoader如何获取已加载的URL?
使用 URLLoader 是否可以获取已加载文件的文件名?
public function loadCSS():void {
var urlLoader:URLLoader = new URLLoader();
urlLoader.addEventListener(Event.COMPLETE, urlLoader_complete);
urlLoader.load(new URLRequest("cssFile1"));
urlLoader.load(new URLRequest("cssFile2"));
urlLoader.load(new URLRequest("cssFile3"));
}
private function urlLoader_complete(evt:Event):void {
// *****How can I get the file name here?
var css:String = URLLoader(evt.currentTarget).data;
// Do lots of stuff
}
Using the URLLoader is there anyway to get the filename of the file that has been loaded?
public function loadCSS():void {
var urlLoader:URLLoader = new URLLoader();
urlLoader.addEventListener(Event.COMPLETE, urlLoader_complete);
urlLoader.load(new URLRequest("cssFile1"));
urlLoader.load(new URLRequest("cssFile2"));
urlLoader.load(new URLRequest("cssFile3"));
}
private function urlLoader_complete(evt:Event):void {
// *****How can I get the file name here?
var css:String = URLLoader(evt.currentTarget).data;
// Do lots of stuff
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
首先,由于
load
方法是异步的,因此代码中的这三个调用将连续相互覆盖。唯一会导致调度COMPLETE
事件的调用是最后一个调用。如果要异步加载文件,则需要为每个文件创建一个 URLLoader 实例。其次,(以及您的问题的更多内容) URLLoader 类,允许您访问最初调用
load()
时使用的URLRequest
。解决这个问题的一个简单方法是扩展
URLLoader
。例如,如果您只需要 url:那么在您的代码中您仍然可以使用单个事件处理程序:
First of all, since the
load
method is asynchronous, those three calls in your code are going to override each other in succession. The only call that will lead to theCOMPLETE
event being dispatched would be the final one. If you want to load the files asynchronously, you need to create an instance of URLLoader for each one.Second, (and more to your question) there are no properties in the URLLoader class that allow you to access the
URLRequest
that aload()
was initially called with.A simple way around this would be to extend
URLLoader
. Eg, if you only needed the url:Then in your code you could still use a single event handler:
创建三个 URLLoader。在完整的函数中,您可以检查事件目标的标识,以确定您从哪一个上获取事件,这将告诉您加载了哪个文件。您也可以使用三个不同的处理程序,具体取决于您想要如何分解代码。
文档并不清楚当您在同一个 URLLoader 上多次调用 load 时会发生什么,这(对我来说)意味着它不是明确定义的行为,您应该避免它。对于您的示例,文档没有指定您的事件处理程序是被调用一次还是三次,以及如果被调用多次,数据是否每次都会不同。
Create three URLLoaders. In the complete function, you can check the identity of the event target to determine which on you're getting the event from, which will tell you which file is loaded. You could also have three different handlers instead, depending in how you want to factor the code.
The docs aren't clear on what happens when you call load multiple times on the same URLLoader, which (to me) means it's not well-defined behavior and you should avoid it. For your example, the documentation doesn't specify whether your event handler will be called once or three times, and if it is called multiple time whether the data will be different each time or not.