Using the previous piece of code we can build a preloader with a progress bar and show the loaded content's percentage.
This should be all you need to know as all the other preloaders are variations of this.
If you understand how to build one, you can build your own.
Create a movie clip - preloader_mc - with:
- a 100 frames animation movie clip inside it - bar_mc
- a dynamic text field inside it - percentage_txt
Actionscript:
-
// create an empty movie clip
-
this.createEmptyMovieClip("my_mc", this.getNextHighestDepth());
-
-
// create the MovieClipLoader Object
-
var mcl:MovieClipLoader = new MovieClipLoader();
-
-
// create the listener Object
-
var listener:Object = new Object();
-
-
// get the percentage
-
listener.onLoadProgress = function(mc:MovieClip, bl:Number, bt:Number)
-
{
-
var percentage = Math.round(bl / bt * 100);
-
// play the animation accordingly
-
preloader_mc.bar_mc.gotoAndStop(percentage);
-
// show the percentage in the text field
-
preloader_mc.percentage_txt.text = percentage + "% loaded";
-
};
-
-
// when it finishes loading
-
listener.onLoadInit = function(mc:MovieClip)
-
{
-
// remove to animation
-
preloader_mc.swapDepths(preloader_mc._parent.getNextHighestDepth());
-
preloader_mc.removeMovieClip();
-
// make the image draggable
-
mc.onPress = function()
-
{
-
this.startDrag();
-
}
-
mc.onRelease = function()
-
{
-
this.stopDrag();
-
}
-
}
-
-
// load the external file:
-
mcl.loadClip("http://www.nunomira.com/tutorials/files_mcl/" + "image.jpg?un="+new Date().getTime(), my_mc);
-
-
// add the listener to the MovieClipLoader Object
-
mcl.addListener(listener);
-
-
// stop the progress bar animation and initialize the text
-
preloader_mc.bar_mc.stop();
-
preloader_mc.percentage_txt.text = "contacting server...";
I've created two more examples.
These examples simulate loading a real website and two possible approaches:
- external.swf has content on the first frame. main.swf has to hide that content.
- external.swf has no content on the first frame. main.swf has to send the playhead to the frame with content after the file finishes loading.
Here's example 1 with the reload button.