Actionscipt
Alright, so let's get down to Actionscript—the most crucial component of a preloader.
xvi. Go back to the "loading_mc" Movieclip. Once inside, insert an blank key frame on the first frame of the layer, select it and press F9 to open the Actionscipt window. Copy and Paste the following code:
- bytesload = _root.getBytesLoaded();
- bytestotal = _root.getBytesTotal()
- percent = Math.floor(bytesload/bytestotal * 100);
- bar.gotoAndStop(percent);
Let me explain this code line by line.
- bytesload = _root.getBytesLoaded();
- bytestotal = _root.getBytesTotal()
We first need to use special Flash functions to check two values: bytes loaded and bytes total. We then assign these values to variables ("bytesloaded" and "bytestotal").
- percent = Math.floor(bytesload/bytestotal * 100);
Then we create yet another variable ("percent"), which is equal to the value of Math.floor*(bytesload/bytestotal * 100).
Math.floor is an in-built Flash function which "brings the number to the floor". It works in opposite way as Math.ceil.
- bar.gotoAndStop(percent);
Remember the bar we have created? This line of code line tells the Timeline inside the very bar to go to and stop on the frame equal to the value of percent. In this way the bar extends as the percent grows. It's actually pretty easy—you are going to find out :-)
xvii. But we still need to write a couple lines of Actionscipt code. Let's see: The code we have created so far tells the bar symbol with an instance name of "bar" to go to this or that frame. That's OK. But what if we cross 100%? The below lines of code take care of this:
- if (percent == 100){
- _parent.gotoAndPlay("loaded");
- } else {
- gotoAndPlay("loop");
- }
The above code tells Flash to keep an eye on the value of the percent variable: If it exceeds 100, Flash "goes to and plays" the actions after the frame labeled loaded, which is located on the main Timeline.
xviii. Alright! It's testing time!
To preview your project either go to Control > Test Movie or press Alt + Enter.
Since we are working locally , we won't be able to see how the preloader works. That's because there is nothing to download.
No worries, though! All we have to do is, while still in the test movie mode, go to View > Download Settings > 28.8. This option will set the stimulated connection speed to 28.8.
|
Once this is done, we can finally test the whole movie. Without leaving the test movie mode, select View > Simulate Download. It should work like a dream now. :-)
Hopefully, after reading this tutorial, you now have a better understanding of how to get preloaders to work properly in Flash. I really hope you have enjoyed this tutorial at the same time.
Cheers!

