Actionscipt
OK, let's get down to actions - this is crucial for a preloader.
16. First we need to get out of the symbol we worked in and return to "loading_mc" or whatever name you chose. Once out of the loading_mc, insert an empty keyframe on the first frame of the layer, select it and press F9 to evoke the Actionscipt panel, 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()
First we use special Flash functions to check two values: bytes loaded and bytes total. We assign those values to variables (bytesloaded and bytestotal).
- percent = Math.floor(bytesload/bytestotal * 100);
Then we create yet another variable (percent) which bears 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 created? This code line tells the timeline inside 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.
17. That's not all as far as Actionscipt goes. The above code tells the bar symbol with an instance name as "bar" to go to this or that frame, but what if we cross 100% border? We need to 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 (it's on the main timeline).
18. OK, time for testing! Control > Test Movie or Press Alt + Enter. We won't be able to see how it works because we are working locally and there's nothing to load. Therefore, 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.
|
When we've got it set, we can finally test the whole movie. Without leaving the test movie mode, select View > Simulate Download. It should work like a dream now. I hope it does!
Hopefully after having read this tutorial, you now have a better understanding of how to get preloaders to properly work in Flash.
Also, don't forget to see other cool tutorials!
Cheers!

