我最终使用了AS3 QueueLoader——它使我能够在加载 XML 后将资产添加到队列中,并且仍然将我的预加载作为一个过程。甜的!
我希望这对某人有所帮助。如果您对此有任何疑问,请告诉我!
下面是一些在 XML 加载后将图像添加到队列中的代码(省略了很多细节):
private function init():void {
_oLoader = new QueueLoader();
_oLoader.addItem(PATH+cssURL, css, {title:'cssContent'});
_oLoader.addItem(PATH+"xml/copy.xml", pageXML, {title:'pageXML'});
_oLoader.addEventListener(QueueLoaderEvent.ITEM_PROGRESS, onItemProgress, false, 0, true);
_oLoader.addEventListener(QueueLoaderEvent.ITEM_COMPLETE, onItemComplete, false, 0, true);
_oLoader.addEventListener(QueueLoaderEvent.QUEUE_PROGRESS, onQueueProgress, false, 0, true);
_oLoader.addEventListener(QueueLoaderEvent.QUEUE_COMPLETE, onQueueComplete, false, 0, true);
_oLoader.execute();
}
private function onItemComplete(evt:QueueLoaderEvent):void {
if (evt.title == 'cssContent') {
css = StyleSheet(evt.content);
}
if(evt.title == 'pageXML'){
pageXML = XML(evt.content);
processXML(); // creates page objects based on XML
for(var i:int=0; i<pageXML.PARENT.length(); i++){
//loops through XML for background images and adds them to various
//sprite layers for simple turning on and off
numSubPages = pageXML.PARENT[i].PAGE.length();
var pageImgHolder = new Sprite();
pageImgHolder.name = 'page'+i;
pageImgHolder.x = 0; pageImgHolder.y = 0;
bgImgHolder_mc.addChild(pageImgHolder);
for(var j:int=0; j<numSubPages; j++){
if(String(pageXML.PARENT[i].PAGE[j].@IMAGE) !== ''){
bgImg = new Sprite();
bgImg.name = 'page'+i+'img'+j;
bgImg.alpha = 0;
pageImgHolder.addChild(bgImg);
_oLoader.addItem(PATH+'images/'+pageXML.PARENT[i].PAGE[j].@IMAGE, bgImg, {title:'page'+i+'img'+j})
trace(pageImgHolder.parent.name+'/'+bgImg.parent.name+'/'+bgImg.name+' = '+pageXML.PARENT[i].PAGE[j].@IMAGE);
}
}
}
xmlLoaded = true;
}
}
private function onQueueComplete(evt:QueueLoaderEvent):void {
trace("** "+evt.type);
imgHolderLoaded = true;
Preloader.instance.spinnerDone();
startMovie();
bgImgHolder_mc.turnOnImg(0, 0);
//turns on image for page 0, subpage 0 (i have a very complicated architecture)
}