Actionscript: Simple Preloader

Some basic actionscript to count the percentage of the flash file loaded, and hold in frame until done.
So, to create a simple preloader in Flash, make a keyframe in frame 2 and put a stop action on it so the Flash holds there. (It has to be frame 2. I always use the first frame to define variables or other basic information which will be used throughout the flash file).

In the second frame, create a movie clip and add the following actionscript onto it:

onClipEvent (load){
total = int(_root.getBytesTotal()/1000)+”Kbs”;
onClipEvent (enterFrame) { = int(_root.getBytesLoaded()/1000)+”Kbs of “+ total; //Divided by 1000 to show Kbs
_root.percent = int(_root.getBytesLoaded()/_root.getBytesTotal()*100)+” %”; //percentage of download complete
if((_root.getBytesLoaded()/_root.getBytesTotal()*100)>99){ // If the percentage loaded=100, go to the next frame, after the stop command

The “onClipEvent (load)” command defines the total size of the flash file to be downloaded.

The “onClipEvent (enterFrame)” defines several variables. First, which shows the total amount of Kbs which have been downloaded at that point (because it’s an enter frame command, it constantly updates the total percent). Second, _root.percent gets an integer (no decimals) of the percent of the file that has been downloaded. Lastly, the looped percentages are checked, and if the percentage is greater than 99 (i.e. 100), the frame moves to the next one, past the stop command which held the preloader in place while the file was being downloaded>

Now, this is just the basic code. Feel free to play around and build it up as elaborately or as simply as you want. You can make you movie clip be some kind of bar or countdown, and you can use any of these variables (or variants of them) to control position, colour, or size at will using simple actionscript commands. For now though, this will get you the basic numbers and setup which holds your framehead in place until the content is fully loaded.

Leave a Reply

17 − 9 =

This site uses Akismet to reduce spam. Learn how your comment data is processed.