First time here? First time here? Newsletter Signup Newsletter
Search tutorials
Author: black

Okay, let's take a look at this line by line to see what's going on.

_root.stop(); This stops the root movie's timeline so as not to try displaying unloaded objects. The next two lines of actionscript are creating variables. We're using strict datatyping here. By declaring what sort of data a variable holds, Flash can check during compilation for mis-assignment and the like, generally saving you a headache here and there. Therefore if I accidentally assign "America is a cultural slaughterhouse" to percLoaded or newPercLoaded, Flash will catch the error and let me know I've muffed up. To assign that to a variable whilst maintaining strict datatyping, I'd have to have a variable of type String... it would look like this:
var americaQuote:String="America is a cultural slaughterhouse";
This datatyping convention also shows up in our preload function. More on that later.

onEnterFrame = preload;
onEnterFrame is a method of the MovieClip class that iterates at the same frequency as your movie's framerate. Here we call the preload function 12 times per second (assuming you haven't changed the framerate).

function preload():Void { This declares a function. It has no parameters and thus the parantheses are empty. the :Void lets the Flash compiler know that this function has no return value.

newPercLoaded = Math.floor(_root.getBytesLoaded()/_root.getBytesTotal()*100);
If you've been looking into preloaders, you've undoubtedly seen this before. This takes the number of loaded bytes, divides that by the whole of the file's bytes and multiplies it by 100. The Math.floor part rounds the insane decimal this yields down to an integer. I store this integer in my variable newPercLoaded.

lb._xscale=percLoaded=percLoaded+(newPercLoaded-percLoaded)*.2; This is the backbone of our smooth preloader. Because newPercLoaded now contains the percentage of the movie that has loaded into the Flash Player and a MovieClip's _xscale property is a percentage of the instance's width, we can use these two in tandem to do anything we please in a nice tidy fashion. So, what happens is I set our bar's _xscale to percLoaded. percLoaded, in turn, is equal to itself plus newPercLoaded minus itself divided by 5. This is syntactically verbose yet not complex. Let's take a disjointed look at what this is doing. Let's say the movie loads instantaneously. This would set newPercLoaded to 100. We've already set percLoaded to 0, and assigned lb's _xscale to percLoaded. So percLoaded = 0+(100-0)*.2, or, computed, 20. So the first iteration of our preload function sets lb's width to 20%. The next iteration would look like this:
percLoaded = 20+(100-20)*.2 or 36. This continues at 12 times per second easing us towards 100, thus eliminating that jerky movement.

if(percLoaded>99.9&&newPercLoaded==100){ this checks whether percLoaded is greater than 99.9 and that newPercLoaded is infact 100. Careful with checking for equality in if statements. One equal sign (=) actually sets what you're checking... so if you check if variable x = 50, it should look like this: if(x==50), not if(x=50). The latter will always return true, thus setting x to 50 and without fail executing the subsequent statements.

stop(); Given we are loaded (see above), we want this clip to stop.

_root.nextFrame; Now that the movie has loaded advance the root movie to the next frame.

delete this.onEnterFrame; We don't want this to continue using system resources so we delete it!

That's it-
» Level Basic

Added: 2005-03-08
Rating: 6 Votes: 32
» Author
black is the new black
» Download
Download the files used in this tutorial.
» Forums
More help? Search our boards for quick answers!
Featured Flash FLA
» Author: Inocreato
» Title: RaiseTheBlocks
» Description: Raise all the blocks to win the game
Featured Sound Loops
Image for equalize

Audio Player

» Author: TomCat Carty
» Title: The Wood
» Description: Just a little game ending or it can maybe be looped. Recorders with music box and percussion to give the feel of well, I don't know, the woods? Free to use, just credit me. thank you
Latest Font
» Author: Fábio FAFERS
» Description: I created this font for free use. Everyone can apply it in personal or business texts. Its free, but I want to be communicated in case of business use. Donations are accepted to keep the project of free fonts alive! Thank you all
Featured Sound Fx
Image for equalize

Audio Player

» Author: Davisigner
» Description: Hmm... what to say about this one? It's reminiscent of the closing notes of the opening music from the Three Stooges done in a church organ style with a closing cymbal crash. I'll give this one away gratis, but feel free to check out my free loops and potential upcoming license-mandated ones over in the respective part of Flashkit.