Although ActionScript 2.0 has been around for quite a few years, I still see many people asking for help when it comes to preloaders ( I can’t really understand why, because there are tons and tons of tutorials and articles all around the web that have completely covered all the preloader issues ).
This post is intended to be a tiny tutorial for everyone who is having a hard time creating a simple ActionScript 2.0 preloader. I won’t be wasting too much time with screenshots and stuff like that, instead, I’ll upload the working preloader ( the file is well commented < at least that’s what I’d like to believe > ) and focus more on explaining why it works ( the logics behind the whole process ):
1) Before starting, we need to define at least two variables that will hold two different things ( the first variable will hold the “total number of bytes that must be loaded” and the second variable will hold the “bytes that have already been loaded” ) but in real life we need at least three variables ( we wouldn’t be able to do too much with two variables, although only two variables are required to create a working preloader )… The first two that I already mentioned and one more that will hold ( after some simple calculations ) the “percentage done”. Let’s name our variables: total_bytes, loaded_bytes, percent_done. I’m sure that the names are intuitive enough.
2) After we have our variables then we can dive into calculating all the stuff that our variables will be holding. Let’s start with our first variable that will hold the total number of bytes that must be loaded. We have a simple function in AS 2.0 that does the exact thing for us, this function is called: getBytesTotal(); Simple enough right? Now that we know the name of the function that will get all those bytes for use, we just have to assign it to our variable: total_bytes = getBytesTotal();
Ok, now we have a variable that holds the number of total bytes that we’ll be loading. Next, we need to get the currently loaded bytes and assign the value to our second variable. Just like with getBytesTotal, we have a function here too that does the job for us, called: getBytesLoaded(); same steps as with tota_bytes but now we have this: loaded_bytes = getBytesLoaded();
Good, I know that this is getting quite boring but hopefully in the end, it will be worth the effort and time ( once you understand how a preloader works you’ll never have to ask for help again, instead, you’ll be the one helping others ).
So, one more variables left. The third variable will first transform the data gathered from the other two variables and calculate the percentage. All this is done with simple math: percent_done = Math.round((loaded_bytes / total_bytes) * 100); ( Math.round is used to round up the number that we’ll get after dividing loaded_bytes by total_bytes and multiplying all that with 100 ).
3) Ok, now we have our variables up and running and with the help of simple math we calculated the percentage ( that we can later display in a Dynamic TextField or wherever we’d like ). Before we’ll have a working preloader, we must do one more thing. We must trigger an onEnterFrame event that is responsible for updating out percentage. If you are not familiar with the onEnterFrame event then I’ll explain shortly what it does: essentially, it is an event ( function ) that is being triggered really fast ( depending on the movies frame rate ), if your movie has a frame rate of 30 fps then the onEnterFrame event will trigger 30 times every second ( theoretically ). Why is this important? Well, if we would have left our variables the way they were then once you start your Flash movie, all the calculations that we did would have been fired only ONCE, and that’s really bad… if they get triggered only once then how can we update the percentage? Here’s when onEnterFrame comes into play, if we place all the calculations that we must do inside the body of the onEnterFrame function then we’ll be having a function that executes all those calculations really really fast. Just download the file I uploaded after you read all this and see how the whole thing looks like ( works ).
4) Good, now we know what onEnterFrame does and we understood that in order to keep our percetange up to date ( and all the other calculations ) we’ll need to place all our calculations inside that onEnterFrame function so that it will recalculate ( update ) the percentage every time it’s triggered.
There’s one more thing we must do to have a working preloader. I said that onEnterFrame will trigger really fast ( depending on the frame rate ) and it will keep updating the percentage and the loaded_bytes, but when will it stop? Well, this is where something else comes into play, called: ifFrameLoaded(); This is a simple function that essentially checks if the frames that we specify are loaded or not. Since we are talking about a preloader, it would be normal to want to check if all the frames of the movie are loaded, this is done like this: ifFrameLoaded (_totalframes) { }; _totalframes is a function that returns the total number of frames of the current timeline it is defined on. So, we are telling Flash to “check if all the frames of the timeline are loaded”, if that’s true then it should do something. First of all, it should remove the onEnterFrame event so that it won’t continue on firing ( once we loaded everything, there’s no more need for an onEnterFrame event that would do sensless calculations ) and we should start playing our movie.
Huh, ok, this got longer than I expected. I hope that after reading all that you guys will understand the logics behind “the preloader”, why it works and what should it have in order to work properly. If you understood all the steps then you’ll only need to download the file I have uploaded and start playing around with it and most importantly reading up on the syntax that you don’t yet understand. I’ve commented the code in my file too so that it will guide you guys.
Hopefully everything is intuitive enough and I hope that I didn’t lose anyone on the road and sorry for the long but hopefully helpful and not too boring post.
The working example can be downloaded from here: Working Preloader Example
( I exported the file for Flash 8, hopefully the file will work for everyone ).
ATTENTION !!! The file that I uploaded covers all the stuff I talked about in this post and I added some additional features too. Beside the simple “display percentage in a text field” feature is has an animated progress bar too ( by using a mask and a shape tween ). I’m quite sure that no one will regret downloading my file and taking a look at it ( especially if you have problems with your AS 2.0 preloader ).
Biro Barna