ActionScript 3: Creating a Preloader for External Content

Here’s the ActionScript preloader code I use, as we as many others. This code is created to load in a separate JPG (or SWF). Notice how it not only show the percentage loaded but also plays an animation as more of the site is loaded.  The code is below or you can download the source file here.

//SETUP
//Make sure there's a MovieClip with an instance name of "preloader_mc" on your stage
//preloader_mc should have an animation that's 100 frames long
//preloader_mc should have another Dynamic Text box in it called loader_txt

//Load in the swf (or jpg or png) file using the Loader object
var swf:String='http://www.designupdate.com/misc/preloader/Budapest.jpg';
var requestSWF:URLRequest=new URLRequest(swf);
var loader:Loader=new Loader();
loader.load(requestSWF);

//Listener object to listen for the progress and
//executes the currentProgress function with each change
loader.contentLoaderInfo.addEventListener(ProgressEvent.PROGRESS,currentProgress);
function currentProgress(e:ProgressEvent):void
{
//This variable get the percentage
var percentage:uint=(e.bytesLoaded/e.bytesTotal)*100;
//Place the percentage number in the text field
preloader_mc.loader_txt.text=percentage.toString()+' %';
//Whatever percentatge is loaded, go to that frame in the preloader_mc timeline
preloader_mc.gotoAndStop(percentage);
}

//Listener object to listen to when the swf is fully loaded
loader.contentLoaderInfo.addEventListener(Event.COMPLETE,completed);
function completed(e:Event):void
{
//Make the preloader invisible
preloader_mc.visible=false;
//Add the loaded swf to the timeline
addChild(loader);
}

error

Enjoy this blog? Please spread the word :)