PDA

View Full Version : Loading swf files and retaining the order of the xml


louisl
11-26-2008, 04:06 PM
I'm using an xml document to get the paths and some variables of the files I need to load. Then I use a loop to populate an array to hold all the data leaving a column empty for the swf/png object once loaded.


public function makeGraphicArray(event:Event):void{

var units:XMLList = event.target.xml.unit;

for (var i:uint = 0; i < units.length(); i++) {

_graphicArray.push({
id:units[i].@id,
file:units[i].@file,
type:units[i].@type,
title:units[i].@title,
body:units[i].@body,
stopframes:units[i].@stopframes,
posx:units[i].@posx,
posy:units[i].@posy,
scale:units[i].@scale,
obj:'loaded file goes here'
});

}

loadUnits();
}


I then loop through the _graphicArray and load each file, depending on the file type I switch loader classes between swf/png.


public function loadUnits():void{

for (var i:uint = 0; i < _graphicArray.length; i++) {
trace(
_graphicArray[i].id + ',' +
_graphicArray[i].file + ',' +
_graphicArray[i].type + ',' +
_graphicArray[i].title + ',' +
_graphicArray[i].body + ',' +
_graphicArray[i].stopframes + ',' +
_graphicArray[i].posx + ',' +
_graphicArray[i].posy + ',' +
_graphicArray[i].scale + ',' +
_graphicArray[i].obj
);

var type:String = _graphicArray[i].type;

switch(type){

case 'swf':
var swf:MovieClipLoader = new MovieClipLoader(_graphicArray[i].file);
swf.addEventListener(Event.COMPLETE, placeGraphic);
break;

case 'png':
var png:BitmapLoader = new BitmapLoader(_graphicArray[i].file);
png.addEventListener(Event.COMPLETE, placeGraphic);
break;

default:
trace(_graphicArray[i].file + ':type was not found!');
break;

}

}

//movieButtons();
//rotateButtons();

}


Now the problem I'm facing is that the placeGraphic function is firing from the listener in a random order depending on how long each file takes to load, whereas I need to know which file has loaded in order to place it in the right spot in the _graphicArray. What I think I need to do is somehow send an ID from the above function.

See - ***NEED THE ID HERE*** below


private function placeGraphic(event:Event):void{

var id:uint = ***NEED THE ID HERE***;

_graphicArray[id].obj = event.currentTarget.getChildAt(0);

var objGraphic:Sprite = new Sprite();
objGraphic.addChild(_graphicArray[id].obj);
objGraphic.x = _graphicArray[id].posx;
objGraphic.y = _graphicArray[id].posy;
objGraphic.scaleX = _graphicArray[id].scale;
objGraphic.scaleY = _graphicArray[id].scale;

if(_graphicArray[id].obj is MovieClip){
_graphicArray[id].obj.stop();
}

this.addChild(objGraphic);
this.setChildIndex(objGraphic, 0);

}


Maybe I'm going about this in the wrong way? any input would be much appreciated.