Create preloader in FlashMX

In this tutorial you will learn how to create a preloader that will return a percentage. This percentage can then be used for things such as a preloader bar. Preloaders that work out percentages are important because they give the users of your site feedback of how much of your flash content is loaded. For this tutorial I expect you to have a basic knowledge of flash and can navigate around freely within the flash interface. I also expect you to have a basic actionscripting knowledge and be able to understand the basic scripts in this tutorial. This tutorial has been writen for use in Flash MX, it takes advantage of some of the added features including text box instance names. If you don't have mx, remember to use the textVariable property instead of the instance name.

Tutorial

1) We'll start off by creating a new movie and adding three keyframes. To do this you can select the first frame and press the F6 key twice. On the first frame make a dynamic text box (text tool>properties> dropdown menu> "dynamic"). Give this text box the instance name of "myOutput". Now copy this text box and paste it in the second frame of the movie. In the third frame put an image or sound file so we will be able to test this preloader latter.

2) Select the first frame and open the actionscript window. If your Actionscript window is set to "Beginner" then click the button in the top right corner of the window and select "Advanced" from the menu, this allows you to freely type and edit code. Now we will write the following code under the first frame

percent = Math.floor(getBytesLoaded()/getBytesTotal()*100);
myOutput.text = percent + "% is loaded";

percent is a variable that we will store how much of the movie is loaded in. getBytesLoaded() retrives the amount of bytes loaded, getBytesTotal() retrives the total size of the movie. when you put these two things over each other and times by 100, you will get the percentage of the movie loaded. The Math.floor() rounds the number to the nearst whole number.

3) In the second frame of the movie clip enter the following code in the actionscript window.

if (percent == 100) {
gotoAndStop(3);
} else {
gotoAndPlay(1);
}

This checks to see if "percent" is equal to 100, if it is, go to the third frame, if its not, goto the first frame.

4) On the fourth frame put a "stop();", this ensures that the movie stops at the last frame and doesn't keep going.