Actionscript Code:
var mcArray:Array = new Array (mc1, mc2, mc3);
var urlArray:Array = new Array ("img1.jpg","img2.jpg","img3.jpg");
loadImages ();
function loadImages ():void
{
//arrayIndex keeps track of the current array position
var arrayIndex:int = 0;
var loader:Loader = new Loader ();
loader.contentLoaderInfo.addEventListener (Event.COMPLETE, imageLoaded);
loop ();
function loop ():void
{
//load an image from a url stored in the urlArray using the current array position (the arrayIndex variable)
loader.load (new URLRequest (urlArray[arrayIndex]));
/*
the loop method gets called for as many urls there are in the urlArray, and is equivalent to doing the following:
loader.load (new URLRequest (urlArray[0]));
loader.load (new URLRequest (urlArray[1]));
loader.load (new URLRequest (urlArray[2]));
etc...
*/
}
function imageLoaded (event:Event):void
{
//create a holder movieclip (for each load complete operation) that can be repositioned within a main movieclip, effectively centring the registration point
var holder:MovieClip = new MovieClip ();
//add the loaded bitmap into the holder
holder.addChild (event.currentTarget.loader.content);
//position the holder using negative values. This moves it from top left of the main movieclip to half of its width and height.
//the alpha is reduced to half so that you can see what is happening
holder.x = -holder.width/2;
holder.y = -holder.height/2;
holder.alpha = .5;
//add the holder to the display list of the main movieclip at the current array position, again, using the arrayIndex variable
mcArray[arrayIndex].addChild (holder);
//check if all the urls have been loaded, if not, increase the arrayIndex value by 1 and call the loop method again to load the next url in the urlArray
if (arrayIndex < urlArray.length - 1)
{
arrayIndex++;
loop ();
}
else //if all the urls have been loaded remove the listener for the complete event just to clean things up
{
loader.contentLoaderInfo.removeEventListener (Event.COMPLETE, imageLoaded);
}
}
}