Since an asynchronous function returns immediately (before loading is done),
you need to pass a callback function (event handler) to get data from it.
该法典可能与此类似:
class Foo {
private var _resources:Array;
private var _loader:Array; // holds URLLoader objects
public get_resource(id:String, callback:Function) : void {
if ( _loader[id] == null ){
var ld : URLLoader = new URLLoader();
// instead of returning value, callback function will be
// called with retrieved data
ld.addEventListener(Event.COMPLETE, function (ev:Event) : void {
_resources[id] = ev.target.data;
callback(ev.target.data);
});
ld.load (/* URLRequest here */);
_loader[id] = ld;
}
// return nothing for now
}
}
<>>[edit] 删除了错误的评论:“/URLLoader物体必须用一个类别变量持有,以避免GC”。 但是,仍然需要载荷来记住的是,哪怕是装载的。