INDIA hitXP.com - Gurudev's Personal website

THE GREAT VEDIC CULTURE Physics - Chemistry - Mathematics - Bio-Sciences - Computers

HOMEHome >> Computers >> Programming >> Action Script

Meet me Online @ Yahoo! - itzguru

Google
Search the web Search hitXP

Writing a pre-loader for a flash movie

It is always a good practice to write a preloader to flash movies whose size is greater than around 20-30 kilobytes. This is because the time it takes for a flash movie to load into the client browser varies according to the client's connection speed, whereas the frame rate of the flash movie (default value being 12 fps) remains a constant and if the next frames to be displayed are not yet loaded then the client browser might display jerky motion or missing movie clips or missing graphics, etc. Hence it is always advised to display the flash movie only after the entire movie is loaded into the browser's cache.

Now to determine whether the flash movie has been completely loaded or not, we have to write a small script (which we shall call the pre-loader script) using the flash scripting language i.e. action script.

For this, first I shall advise you to have a small animation like the one shown below to be drawn in the first three frames of the first scene of the movie. Your actual movie can start with the 4th frame of the first scene.

A Simple pre-loader animation

Then you can write the pre-loader script in the action part of the third frame. I assume that you know where to write an action script for a given frame in a flash movie. You have to just right click on the required frame and select 'Actions' to invoke the action script window.

Now, there are two ways to write an action script for a pre-loader.

  1. Pre-loader based on the number of frames present in the movie.

  2. Pre-loader based on the size of the movie.

Pre-loader based on the number of frames present in the movie

The action script based on the above idea is as follows:
if( _framesloaded = = _totalframes){
 gotoAndPlay(4);
} else {
 gotoAndPlay(1);
}

This script should be placed in the action window of the 3rd frame. This script compares the number of frames loaded into the browser cache with the total number of frames in the movie. If the number of frames loaded is equal to the number of total frames present in the movie then it implies that the entire movie has been loaded and hence the control is passed to the next frame i.e. the 4th frame (remember that the actual movie had to be started from the 4th frame). If the number of frames loaded is still less than the total frames in the movie then the control is passed back to the first frame to continue showing the "loading..." animation. This back and forth swing between the third and first frames continues till all the frames in the movie are loaded after which the control is passed to the 4th frame from where the actual movie starts.

Pre-loader based on the size of the movie

Here is another pre-loader in action script which is based on the size of the movie, rather than on the number of frames of the movie.

if( getBytesLoaded() >= getBytesTotal() ){
 gotoAndPlay(4);
} else {
 gotoAndPlay(1);
}

The only difference between the previous action script and the above action script is that the above action script compares the size of the movie loaded with the total size of the movie file (i.e. the swf file). The inbuilt action script function getBytesLoaded() returns the actual number of bytes of the flash movie loaded in the browser cache, while the getBytesTotal() returns the size of the flash movie in bytes.

If you want to display the percentage of the flash movie loaded in your pre-loader then you should use the second script i.e. the script based on the file size rather than using the script based on the number of frames in the movie. This is because different frames may have different sizes and hence will not present an accurate picture of the percentage loaded if you use the script based on number of frames to display the percentage of the movie loaded.

here is an action script which calculates the percentage of movie loaded.

var percentage;
percentage=Math.ceil((getBytesLoaded()/getBytesTotal())*100);

-by Gurudev
MADE IN INDIA

gurudevp@vsnl.net

On 27 November 2002

HOMEHome >> Computers >> Programming >> Action Script

Rate this article

PoorExcellent       

Comment on this article: Criticism is most welcomed
Name:
email ID:
Comments:

Physics - Chemistry - Mathematics - Bio-Sciences - Computers