Creating A Simple Preloader In As3
For all you that have been follow this blog from the start, you’re probably up to the point where you can create games and applications. For the large of these loading times can be a problem. After all, the chances of someone sitting there looking at a black screen for a minute while you’re game loads is unlikely.
This is where a preloader comes in, for everyone that doesn’t know, a preloader is a small piece of code that executes almost instantly and tells you how long until the rest of the game or app is loaded.
In this tutorial, i will show how to make one.
Step 1
Create a dynamic text field. Give it an instance name of percentComplete
Step 2
In code view, add these 2 lines:
this.stop();
These will the our app from continuing while loading commences.
Step 3
Add an event listener that is triggered each time part of our app loads and one for loading completion:
this.loaderInfo.addEventListener(Event.COMPLETE, loadComplete)
Step 4
Create our function and work out the percentage complete:
var percent:Number = (e.bytesLoaded / e.bytesTotal) * 100;
To work out the percentage complete will take the amount of app we have loaded and divide it but the total size of the app. Then times by 100.
Step 5
Add in a line to set the text of the textfield underneath the previous:
Step 6
Create a function to handle the loading completion and move to the next frame.
{
this.removeEventListener(ProgressEvent.PROGRESS, updatePercent)
gotoAndStop(2)
}
And there you have it, a simple preloader in AS3





