AS: Preloader
Basics
To create a basic preloader, you need two variables:
_root.getBytesTotal(); //grabs the size of the swf (in bytes)
_root.getBytesLoaded(); //grabs the amount of swf loaded (in bytes)
There are various ways to go about using these variables. In the simplest form you would just need to creat a Dynamic Text Box on frame one of your movie, give it the Var "percent".
On the first frame of your movie, add these actions
var bytes = _root.getBytesTotal();
var bytes_loaded = _root.getBytesLoaded();
percent = int((bytes_loaded)/(bytes)*100) + "%";
if (bytes_loaded == bytes) {gotoAndPlay(3)}
And on the second frame, gotoAndPlay(1)
This assumes that your movie starts on frame 3.
To test your preloader, press CTRL+ENTER twice. You don't have to upload it to test it!
While in the swf window, use VIEW>DOWNLOAD SETTINGS to change the simulated bandwidth.
------------------------------------------------------------------------
Using an MC
It's generally a bit tidier, and more flexible to use an MC. Create 2 Dynamic Text Boxes, and give them the Vars "pl1" and "pl2".
Select them both, convert to Movie Clip (press F8).
Create a blank keyframe on the timeline at Frame 2, and add a stop(); action
Drag an Instance of the MC to the stage. Select it, and add these actions.
onClipEvent(load){
totalb=_root.getBytesTotal();
totalkb = int((totalb)/1024);
}
onClipEvent(enterFrame){
var loadedb = _root.getBytesLoaded();
if(loadedb == totalb){
_root.gotoAndStop(3); //If movie is loaded
}else{
loadedkb = int(loadedb/1024);
percent = (loadedb/totalb)*100;
pl1=""+int(percent)+"%";
pl2="" + loadedkb + " / " + totalkb + " kb";
}
}
------------------------------------------------------------------------
Variations
By using the percent calculation, you can do all sorts of cool things. Here are a couple of fairly uncomplicated ones:
Progress bar
Create a new MC. Inside it, draw a long thin rectangle. Set the bar's left-hand end to x=0, y=0 in the PROPERTIES bar.
Drag a copy of the bar to stage (on the same frame as your preloader MC, usually Frame 2), and give it the Instance Name: "progbar"
Change the actions on the preloader MC:
onClipEvent(load){
totalb=_root.getBytesTotal();
totalkb = int((totalb)/1024);
barwidth=_root.progbar._width;
}
onClipEvent(enterFrame){
var loadedb = _root.getBytesLoaded();
if(loadedb == totalb){
_root.gotoAndStop(3); //If movie is loaded
}else{
loadedkb = int(loadedb/1024);
percent = (loadedb/totalb)*100;
_root.progbar._width=(barwidth/100)*percent;
pl1=""+int(percent)+"%";
pl2="" + loadedkb + " / " + totalkb + " kb";
}
}
There's no reason why you can't place the progbar inside the preloader MC, and drop the _root from both of the italicised lines - the choice is yours
Progress animation
Create a 100-frame progress animation (eg, a clock face going from 12 to 12)
Place it on the stage (on the same frame as your preloader MC, usually Frame 2), and give it the Instance Name: "planimation"
Change the actions on the preloader MC:
onClipEvent(load){
totalb=_root.getBytesTotal();
totalkb = int((totalb)/1024);
}
onClipEvent(enterFrame){
var loadedb = _root.getBytesLoaded();
if(loadedb == totalb){
_root.gotoAndStop(3); //If movie is loaded
}else{
loadedkb = int(loadedb/1024);
percent = (loadedb/totalb)*100;
_root.planimation.gotoAndStop(int(percent));
pl1=""+int(percent)+"%";
pl2="" + loadedkb + " / " + totalkb + " kb";
}
}
I think that's enough examples. You can do all sorts of other things using the percent, by including it in equations with Stage.width, Stage.height, _alpha, _height, _width, _x, _y, etc. Remember, the preloader is the first impression that viewers will have of your Flash, so it's worth spending a few hours creating a smart, original one.
------------------------------------------------------------------------
Tip: I recommend placing your Preloader MC on frame 2 of a movie, and placing a simple text "Loading" on frame one. This means that the viewer will be able to see something while the preloader is loading, rather than a blank white rectangle.
More preloader tutorials:
http://www.newgrounds.com/portal/view.php?id=175909
http://www.newgrounds.com/portal/view.php?id=39399
http://www.bestflashanimationsite.com/tutorials/3/
http://www.actionscripts.org/t...r/Smooth_PreLoader/index.shtml
AS: Sections
AS: Main
AS: Basic Movement
AS: Random Movement
AS: Sound