Click here to download a custom ActionScript 3.0 preloader that you can use in your projects. Very easy to use. The code is fully commented and is also reviewed below. First, make sure there’s a MovieClip with an instance name of “preloader_mc†on your stage. This preloader_mc should have an animation that’s 100 frames long as well as a Dynamic Text box in it called loader_txt. Next, load in the swf (or jpg or png) file using the Loader object:
var swf:String=’http://www.designupdate.com/architectingflash/index.swf’;
var requestSWF:URLRequest=new URLRequest(swf);
var loader:Loader=new Loader();
loader.load(requestSWF);
//Listener object to listen for the progress and execute the currentProgress function with each change. The function places the percentage in a text field and goes to the frame number that equivelent to the percentage.
loader.contentLoaderInfo.addEventListener (ProgressEvent.PROGRESS,currentProgress);
function currentProgress(e:ProgressEvent):void
{
var percentage:uint=(e.bytesLoaded/e.bytesTotal)*100;
preloader_mc.loader_txt.text=percentage.toString()+’ %’;
preloader_mc.gotoAndStop(percentage);
}
//Listener object to listen to when the swf is fully loaded so you can make the preloader_mc invisible and add the loader to the main timeline.
loader.contentLoaderInfo.addEventListener(Event.COMPLETE,completed);
function completed(e:Event):void
{
preloader_mc.visible=false;
addChild(loader);
}