If part or all of your Flash movie will require significant load time for some users--for instance, if you use a lengthy background sound or a large number of bitmaps--you should consider building a preload sequence at the beginning of your movie.
There are many ways to design such sequences, but most if not all will contain these two ActionScript statements:
- A test to determine if a certain frame has loaded, using the ifFrameLoaded() method;
- An instruction to return to the first frame.
The two statements are executed in this order. If the first condition is not met, the movie loops. Once the target frame has been loaded, the movie continues to a frame specified in the ifFrameLoaded() instruction.
The demonstrations above illustrate two fairly simple uses of this technique.
Preloader 1
In the first example, a sequence of 21 frames precedes the main action, which begins at frame 25. These frames contain a scale and color change effect on a text box containing the message "Loading..." The following script is installed on frame 20:
ifFrameLoaded(239){
gotoAndPlay(25);
}
Note there is no handler involved, since we're scripting the frame directly. The argument to ifFrameLoaded, 239, indicates the last frame of the movie, which contains a large number of bitmapped images and so takes some time to load. The gotoAndPlay() statement executes only if this final frame has been loaded.
If the final frame has not yet been loaded, we proceed to frame 21, which has the following script:
gotoAndPlay(1);
This statement creates a flexible loop.
Preloader 2
This example adds a counter that indicates what frame is currently being loaded. The most elegant solution here would be a progress bar or other graphic indicator; for quick and simple illustration, though, this demo uses a text field.
The architecture is similar to the first example. Frames 1 and 2 make up the preload sequence. Each of these frames contains the display field loadCount and its associated message. Frame 1 has this script:
for(x=0; x<224; x++)
{
ifFrameLoaded(x) howFar = x;
}
_root.loadCount = howFar;
ifFrameLoaded(224) gotoAndPlay(15);
The for loop examines every frame in the movie and determines if it has been loaded yet. When a frame has been loaded, the value of the variable howFar is set equal to that frame number. When all the frames have been checked, the value of howFar is displayed in the loadCount field.
Next comes the ifFrameLoaded() test, which branches off to frame 15 if the last frame of the movie has been loaded. If the test is not satisfied, we proceed to frame 2, which uses goToAndPlay() to create a loop.
The source files for these demos, preloader1.fla and preloader2.fla, are available on Crow in multimediaShare/preloaders.
