Monitoring Loading

I l @ ve RuBoard

All Flash movies stream. This means that the first frame of a Flash movie starts playing as soon as it is ready, no matter how far off the last frame is from loading.

You may not want this to happen. If your entire movie is a quick animation, you may not want to start until the entire movie has been downloaded from the Web and is waiting in the user 's browser cache to be played .

There are several ways to force the movie to wait for loading to complete. The most common way is to have a loader frame. This is the first frame of the movie. It watches certain properties of the movie to determine when the movie is finished loading.

To find out how many frames of a movie have been loaded, you can use the _framesLoaded property of the movie. You can compare this to the _totalFrames property to see how far along the loading is.

You can use this in some simple cases. For instance, you can place a stop() on the first frame of your movie. There can be a button that allows the user to continue. When the user clicks the button, you can use a script like this to determine what to do next :

 on (release) {     if (_root._framesLoaded == _root._framesTotal) {         play();     }  else {         textDisplay = "Wait a few seconds and press again.";     } } 

If the entire movie is not ready, the text field linked to the variable textDisplay informs the user of the delay.

You could also use this technique on parts of a long movie. For instance, this button can be on frame 50 and let the user continue only if the next 50 frames are ready. A script attached to a small movie clip can monitor the status and move the movie forward automatically when the time is right.

 onClipEvent(load) {     _root.textDisplay = "Waiting for the next sequence to load.";     _root.stop(); } onClipEvent(enterFrame) {     if (_root._framesLoaded >= 100) {         _root.play();     } } 

This is the heart of a basic loader script. However, there are more accurate ways to monitor loading than by frames. You can use the getBytesLoaded and getBytesTotal functions to find out the actual size of the file and how much has been loaded.

Here is a script that sits on a movie clip in the first frame of a movie. The first frame should also have a stop() command on it.

 onClipEvent(enterFrame) {     if (_root.getBytesLoaded() == _root.getBytesTotal()) {         _root.play();     } } 

Because this script runs once per frame, it is constantly checking the two functions against each other. The statement becomes true the moment the movie is completely loaded. Then the play command moves the movie forward.

I l @ ve RuBoard


Sams Teach Yourself Flash MX ActionScript in 24 Hours
Sams Teach Yourself Flash MX ActionScript in 24 Hours
ISBN: 0672323850
EAN: 2147483647
Year: 2002
Pages: 272

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net