Flash movies stream by default. However, it is not always desirable to have a flash file begin to play until it is fully downloaded. This is where pre-loaders (or just "loaders") come in. Pre-loaders are "mini" animations that download quickly and play in the "foreground" while the main file downloads in the "background." Some preloaders contain little animations to entertain the user, others shown a progress bar which lets the user know how much of the movie has downloaded (I like these best). With this exercise we'll create a couple different kinds from simple to more complex.
This loader just plays a little animation until the movie is fully downloaded.
onClipEvent(enterFrame) {
if (_root.getBytesLoaded() == _root.getBytesTotal()
) {
_root.play();
}
}
This action compares the amout of imformation downloaded (getBytesLoaded) with the total number in the file (getBytesTotal). Once they are the same (==), the movie plays.
Try it, but be warned, if it is a small .swf file you might not see much of the loader other than a really quick flash.
This loader adds some text that tells the user what percentage is downloaded thus far.
onClipEvent(enterFrame) {
bytesLoaded = _root.getBytesLoaded();
bytesTotal = _root.getBytesTotal();
percentLoaded = Math.round(100*bytesLoaded/bytesTotal);
_root.displayText = "Loading: "+percentLoaded+"%";
if (bytesLoaded == bytesTotal) {
_root.play();
}
}
This script first creates two variables called "bytesLoaded" and "getBytesTotal." Each is then assigned a value (the number of bytes loaded so far, and the total number of bytes, respectively). Next a "percentLoaded" variable is created. Inside this variable a mathematic function divides the amount loaded by the total number of bytes to find the percentage downloaded so far. This amount is then written into the dynamic text field. As the movie plays this script is executed over and over again; each time the numbers are updated, so the percentage grows until it hits 100%. Then the main movie starts.
This loader adds a graphical display—a progess bar that grows as the movie loads.
onClipEvent(enterFrame) {
bytesLoaded = _root.getBytesLoaded();
bytesTotal = _root.getBytesTotal();
percentLoaded = Math.round(100*bytesLoaded/bytesTotal);
displayText = "Loading: "+percentLoaded+"%";
barFill._xscale = percentLoaded;
if (bytesLoaded == bytesTotal) {
_root.play();
}
}
There are two differences from the previous action. First, the "_root." has been removed from the "displayText" command. The reason is that we've moved the dynamic text box into the movie clip that contain this script, so we only need what amounts to a direct, relative link. When the text field was on the main timeline, we had to reference it by its absolute path name; "_root" means that it is on the root level.
The second difference is the addition of the "barFill" line. This line causes the barFill movie clip to expand as the movie loads. Pretty slick, eh?
Test it out. You probably won't see much. However, if you upload a .swf with a loader to the server and view it there, you may see the loader in action.