Back to Entry Page

Preloading

Screenshot from 'Sleeper' demo

I. Problem

Once you begin to include long frame sequences such as video and animation in your Flash movies, you'll quickly notice large file sizes. Flash movies will stream--that is, play as they load--but streaming can produce unintended effects, for instance when playback stops to wait for the next bit of data to arrive. If you need all your movie's content to be available before play begins, you have to hold the movie at a certain point and make it wait there until all frames have been loaded. Here's how I added such a feature to the earlier "Sleeper" demo.

II. Preloading

First a few changes were necessary to the timeline architecture. I added a second scene to the main "Sleeper" movie, renamed the original Scene 1 as "Scene X", named the new scene "Scene 1", renamed "Scene X" as "Scene 2", then reversed their order in the Scenes panel. The new Scene 1 (the scene I added) provided the place to do the preloading work.

I created two frames in the Scene 1 timeline and placed the following script on the second frame:

if(_framesLoaded >= _totalFrames){
    gotoAndPlay("Scene 2", 1);
    {
else{
    _root.firstView._alpha = (_totalFrames/_framesLoaded)*100;
    gotoAndPlay(1);
	}

This frame action script (note the absence of a handler) refers to two properties of the present movie: _totalFrames, which indicates the total number of frames in the movie, and _framesLoaded, which indicates how many frames are currently stored in memory. Yes, Flash knows how many frames a movie contains even before it loads them all. Comparing the two values allows us to hold off full interactive engagement until all the content is in place.

When fewer than the total number of frames have been loaded, the script does two things. First it sets the alpha of a Movie Clip named "firstView" to the percentage of frames that have been loaded. "Firstview" is identical to the first frame of the main animation. As more of the movie loads, this graphic becomes more opaque. Once this alpha adjustment has been made, the script loops back to frame 1 of the present scene (Scene 1), which in turn succeeds to frame 2, re-running the script. I'm not sure it's necessary to use two frames here, but it seems safer to do so.

In the actual version of the project you'll also see a pair of rotating gears meant to suggest something like a film projector. These are added as a second visual index for the loading process.

Once all frames of the movie have been loaded we go to the second scene (the main scene) and everything runs as before. I did have to make one small but important change to the general scripts. You'll recall that there is a script to advance the action once we've come to a stop point (the script triggered by the right-facing arrow). Previously that script used the statement:

  gotoAndPlay(_root._currentFrame+1);

In the revised version it reads:

  _root.gotoAndPlay(_root._currentFrame+1);

The change is necessary because the frame in which this script resides is now within Scene 2, not Scene 1 as it was originally. The goto method is very sensitive to context.

III. Source file

The source file for the revised "Sleeper" project (with preloading) is called sleeper03.2.fla. It can be found in MMShare/sleeper03/ on Crow.


University of Baltimore Logo

Copyright © 2002 School of Information Arts and Technologies