Creating a Numeric Bar Preloader in Flash

By Blue_Chi | Flash MX / Flash 8 | Beginner

Having a preloader is necessary in all Flash movies displayed on the web as they stop the movie from being played before loading all the data and it also informs the user how much data has already loaded. This tutorial will teach you how to create a very simple numeric bar preloader that could be used in almost any movie. You are not required to have any special ActionScript knowledge to be able to follow this tutorial.

End Result

How does it work?

The idea behind a preloader is pretty simple, we are using ActionScript to retrieve the number of bytes that has been loaded and then use this value to create a percentage that is assigned as the width of the loading bar. This tutorial will teach you how to do this in Flash.

Starting off

Start off by creating a new flash movie, you can use the default settings, 550x400 px for the dimensions, 12 fps as the framerate and white the background.

Stage Setup

Setting up the Timeline

Create two layers on the timeline, name the upper one Actions and the one below Contents. Right-click the second frame on the upper layer (Actions) and select Insert Keyframe, and then Right-click the second frame in the lower layer (Contents) and select Insert Frame. This makes the contents of the lower layer span across the first and the second frame, while the upper layer has different contents in the first and the second frame.

Timeline Setup

Creating the Bar

We are going to put our bar in the Contents layer, so click once on the name of the layer labeled Contents and then use the Rectangle Tool to draw in the center of the stage a bar similar to the one illustrated in the image below, you'll have to check before you make your bar that there is a fill color and an outline colour selected in the lower part of the toolbar.

Rectangle Tool Colors Bar

We will now have to transform our bar into a movieclip that we can later manipulate using ActionScript. We are going to separate it from its outline so that the outline remains unaltered. Use the Selection Tool to select ONLY THE OUTLINE of our bar.

Selection Tool Outline Selected

We will now transform our outline into a movie clip. Simply hit F8 on your keyboard to show the Convert to Symbol window, select Movie clip and name your symbol Outline.

Conver the outline to a Symbol

That should do the outline part, select the colored bar body now using the Selection Tool and hit F8 to open the Convert to Symbol window. We are going to change the Registration Point, i.e. the place from which our bar is going to grow bigger, select the point on the left side of the square. Select Movie clip, name the symbol Bar and then hit OK.

Bar Converted into a Symbol

A small point that we need to do now is make sure the outline appears above the bar, because of the order in which we created the objects, the outline would be lower because it was created earlier. Simply use the Selection Tool to select the bar symbol and then hit Ctrl and the Down Arrow to make the bar go below the outline.

Bar Below Outline

This is our final step for the bar, while the colored bar body is still selected, access the Properties Inspector and assign an instance name to it. This makes it possible for us to manipulate this object using ActionScript. Use the instance name bar_mc.

Properties Inspector Instance Name

Creating the Text Field for our Numeric Preloader

This one is easier, use the Text Tool to create a text field below the bar and then access the Properties Inspector to change the Text Type to Dynamic and assign the instance name loader_txt to it. I have set the font to _sans, the font size to 12px and the color to black.

Text Tool Text Field Text Type

Adding the ActionScript

Right-click the second frame on the upper layer labeled Actions and select Actions. Copy and paste the code below to make our preloader functional.

if (_root.getBytesTotal() != _root.getBytesLoaded()){
gotoAndPlay(1);
}
bar_mc._xscale=(_root.getBytesLoaded()/_root.getBytesTotal())*100;
loader_txt.text=Math.round((_root.getBytesLoaded()/_root.getBytesTotal())*100)+"%";

Note: You can learn more about the 'if' conditional by reading this tutorial.

The first part of our code tells the movie to go back to the first frame if the amount of data loaded does not equal the total amount of the data the file contains. We later set the horizontal scale of our bar to the percentage of data loaded, and then used the rounded value as the text value of our text field.

Our preloader is now ready to use, you can insert a keyframe on the forth frame of our content layer and then import a large image to the stage by going though File>Import>Import to Stage. You can then test your movie Control>Test Movies and then simulate download by going through View>Simulate Download.

End Result

This concludes our tutorial. I hope that you learnt how to create a preloader that you could use for all of your flash movies, you can download a sample FLA of the end result through this link. Please feel free to post your comments, questions or suggestions at the Oman3D Forum.

- End of Tutorial.

x