虽然我的代码非常庞大且分散在许多文件中以显示在这里,但我基本上归结为:
我有一个这样做的功能:
var loadedScript:Loader;
function load():void{
loadedScript = new Loader();
// loadedScript is initilized somewhere and other stuff
loadedScript.loadBytes(bytes, context);
loadedScript.contentLoaderInfo.addEventListener(Event.COMPLETE, scriptLoaded, false, 0, true);
}
当我连续两次调用这个函数时,它调用了loadScript.loadBytes(bytes,context)之前的2.时间;从1.时间可以完成,然后“scriptLoaded”方法只被调用2.时间,而不是1.时间
那么,这是有意的行为来自loadedScript.loadBytes(bytes,context);方法,还是一个虫子,我能以某种方式绕过这个吗?
答案 0 :(得分:2)
你可以创建一个加载器队列,如下所示:
var queue:Array = new Array();
function load(bytesToLoad:*):void {
queue.push(bytesToLoad);
processQueue();
}
function processQueue():void {
//check if items are on queue
if (queue.length > 0) {
//create a loader
var ldr:Loader = new Loader();
//add event for loading complete
ldr.contentLoaderInfo.addEventListener(Event.COMPLETE, scriptLoaded, false, 0, true);
//load the bytes (get first item from the queue, which contains the bytes to load)
ldr.loadBytes(queue.shift(), context);
}
}
function scriptLoaded():void {
//do stuff
//process next item in queue
processQueue();
}