Flash Explained

Learn Flash easily.

Basic internal percentage preloader

October 8th, 2008 | Author: Luka | Category: Preloaders

With this tutorial, you will learn how to make a basic preloader which shows how much of the Flash movie in which it resides has loaded. This kind of preloader is best used with small movies, intros and the like.

Setting up the scenes

First, we will make two scenes, to separate the preloader from the actual content of our flash movie. By doing so, it will be easier to make any changes to the content or the preloader later, if need be.

1 Open a new Flash document. Press SHIFT+F2 or choose Window > Other panels > Scene. This will open the Scene panel. You will see Scene 1 inside it, which is the scene we are currently in. Double-click on its name and call it preloader. You will see its new name appear above the Layers, on top of the Timeline panel.

2 Still in the Scene panel, click on the Add scene button, which is situated at the lower right corner of the panel, marked with a small plus symbol. You will create a new scene. Double-click on its name and call it content.

The scenes in a Flash movie are parts of the same timeline. They exist to make editing a movie easier. Imagine having a big flash movie with an intro, credits, opening scene, second scene, etc. Now imagine having all that on one single scene! If anything needed to be adjusted, especially animation, your work would be made impossible by having to adjust the keyframes back and forth by pushing them a couple of hundred frames at a time.

The scenes play in a movie sequentially, the top scene in the Scene panel being the first to play, followed by others beneath it. If you want, you can close the Scene panel now, we won’t be needing it anymore.

3 We need to put some material in our content scene to make our filesize a little bit bigger, otherwise we won’t be able to see the preloader at work, if everything loads instantly. To that end, an image will be sufficient. Press CTRL+R or choose File > Import > Import to Stage, find an image with the filesize about 80 Kb – 100 Kb (also, if you choose a JPEG, be sure it’s a standard, NOT a progressive JPEG) and click OK.

Call the layer we just placed an image in site content or image or whatever you like. Lock this layer.

4 Test your movie by pressing CTRL+ENTER. If you did everything up to this point correctly, you will see the image flash on screen. Now why is that happening? The first scene, preloader, shows briefly on the screen with nothing in it, followed immediately by the content scene with the image, and then the whole movie loops, until we close it. So, this would remain the same even after we finish the preloader. In order to avoid this, we need to stop the movie at the content scene.

5 Still in the content scene, make a new layer and call it actions or actionscript and lock it (you can put ActionScript in a layer even if it’s locked). Now, click on its first frame and press F9 (ALT+F9 on a Mac) to open the Actions panel. Type in the following cline of code:


and test your movie again. It should show the image, with no flashing.

Creating the preloader

6 Now go back to the preloader scene. To do so without the Scene panel, go to the little scene icon situated on the top right above the timeline (see image below). Click on it, and select the preloader scene from the small pop-up menu.

Selecting a scene.

7 Next, we will create a dynamic text field which will display the percentage of the movie loaded. To do so, select the Text tool (T) from the Tools panel. Click and drag on the stage to create a text field. By default, Flash will create a Static text field. To change it to a dynamic text field, go to the Properties panel below the scene and you will find a small drop-down menu on its left side. Choose Dynamic Text. Next, select a font size in the same panel, and the color. Be sure to select any color but white, as our movie has a white background.

Below the menu where we defined our text field as a dynamic one, you will see a small input field called Instance Name. This is where we’ll put the name of our text field. This is necessary if we want to control the text displayed in our text field via ActionScript, dynamically. Let’s call it infodisplay. Type this in and be sure to press ENTER before clicking anywhere else.

Naming the text field.

Now, we will make our movie loop inside the preloader scene, until all the content is loaded. Once that is accomplished, we will tell Flash to go to the content scene.

8 Call the layer with our dynamic text field textfield and lock it. Create a new layer and call it actions. Lock this layer too. In this layer, place your mouse over the frame no.2, right-click and choose Insert Keyframe from the pop-up menu.

In the layer textfield go to the second frame also, right-click and choose the Insert Frame option from the pop-up menu. You should have the situation in your Flash movie match the image below.

The timeline with layers.

9 Choose the second keyframe in the actions layer (left-click on it) and open the Actions panel (Window > Actions). Type in the following code:


As a proof of having placed the ActionScript code in the right keyframe (keyframe 2 in the actions layer in our case), you should see a small letter “a” displayed in it. This small piece of ActionScript code will keep the movie looping in the preloader scene. It basically tells Flash to return to frame 1 and continue playing. Try it – test the movie and you won’t see the image in the content scene, but just a blank screen. This is ok, because we don’t have anything in the preloader scene yet, except a blank text field which isn’t visible because there isn’t any text in it yet.

10 Now, click in the first keyframe of the actions layer and enter the following code, just to make sure that we can properly control our infodisplay text field dynamically with ActionScript.

infodisplay.text = "testing 123";

Test your movie and you should see the text “testing 123” displayed on the screen, in our text field. The line of code above, translated into human language, literally tells Flash “The text in the textfield named infodisplay should be what you see between the quotation marks: testing 123.”

11 If everything works fine, you can delete this line of code we just wrote. If you have problems displaying the text in the text field, check that:

a) your text field is dynamic, not static – do this by unlocking the layer textfield, selecting the textfield with one click and looking at the left side of the Properties panel,

b) your text field is properly named – the name which appears in the Instance Name field in the Properties panel should be the same as in the line of ActionScript code we wrote to test it. Beware that Flash is case-sensitive! A text field named infodisplay is not the same as Infodisplay or infoDisplay. Also, you can’t have any spaces or special characters in the name.

c) you didn’t choose white as the colour of your text field.

12 Now, let’s make our preloader work. Type in the following code (in the first keyframe of the actions layer):

movieLoaded = this.getBytesLoaded();
movieTotal = this.getBytesTotal();
percentage = movieLoaded/movieTotal*100;
infodisplay.text = percentage;

Test your movie – press CTRL+ENTER (PC) or COMMAND+ENTER (MAC). You will see the number 100 displayed. While still having your movie running, press CTRL+ENTER (PC) or COMMAND+ENTER (MAC) again. This will test your movie again, but it will simulate a 56K modem downloading your movie from the Internet. If you are a mouse clicker, you can do this by choosing View > Simulate Download while your movie is running, and choose which speed to simulate by going to View > Download Settings.

OK, once you did that, you certainly noticed that the text field displayed the percentage of the movie being downloaded so far. But, the percentage wasn’t a round number, but rather a floating-point one. Before we proceed to round our percentage, let’s first explain the above ActionScript code, line by line.

movieLoaded = this.getBytesLoaded();

The above line creates a variable called movieLoaded, which stores the information on how much bytes of our Flash movie have been loaded so far. The getBytesLoaded command fetches the number of bytes which have been loaded so far. The keyword this tells Flash that the command getBytesLoaded is to be applied to the timeline on which the keyword itself resides. OK, you say, but, please, repeat this in human language 🙂 Here is the explanation:

When the keyword this is written in a keyframe on a layer, it indicates that what will follow it will be applied to this same timeline. In our case, the keyword this is written on the main timeline of our Flash document, which means the keyword indicates the Flash document itself. That is why the movieLoaded variable will hold the value of our Flash document bytes loaded so far.

movieTotal = this.getBytesTotal();

This line of ActionScript code is similar to the one preceding it. It creates a variable called movieTotal, and stores the total weight of our Flash movie inside it, again, in bytes.

percentage = movieLoaded/movieTotal*100;

We need the information on how much bytes of our Flash movie have been loaded so far and how much bytes our movie weighs in total, so we that we can calculate a percentage of the movie loaded so far, to display in our text field. Why a percentage? Well, the visitors to your site aren’t interested in bytes, we can freely assume. A percentage is a clear and simple indicator of how much more they must wait in order to see the Flash movie or site they want to see.

So, a variable called percentage is created (you can call it loadedSoFar, ratio, displaypercentage or whatever you like, the important thing is to stick with that name throughout your document) . This variable holds the numerical value of how much percent of our movie has been loaded so far. How? By doing a simple mathematical operation. We divide the part (movieLoaded) with the whole (movieTotal) and multiply the result by 100 and we get the percentage. We will explain how to round the resulting value in a moment.

infodisplay.text = percentage;

This ActionScript line tells Flash to display the value stored in the percentage variable in the infodisplay text field. Cool! Let’s move on!

13 Modify the line containing the percentage calculation as follows (the modifications to the code are displayed in bold):

percentage = Math.round(movieLoaded/movieTotal*100);

Don’t forget the parenthesis at the end! OK, so what this does is: the Math object is given a method (a method is a thing an object can do in a programming language), in this case, round. This is followed by the percentage calculation within parenthesis. The Math.round piece of code will round whichever number it finds in parenthesis following it. Test the movie (by repeating the keyboard shortcut twice, as explained at the beginning of part 11 of this tutorial) and you’ll see the rounded percentage, i.e. a whole number displayed.

We got the percentage of our Flash movie being loaded displayed correctly, but a number in itself may be insufficient. Let us give our visitors some more information, like: Loaded: 100%. To do this, modify the last line of our code, which tells Flash what to display in the infodisplay text field. Again, the modifications to the ActionScript code are shown in bold:

infodisplay.text = "Loaded: " + percentage + " %";

Notice the space after the word loaded:. Ommiting the space won’t cause our code to malfunction, but it will look ugly in our text field when displayed. Try it and see for yourself. Test the movie and see the result.

If you don’t see the whole text displayed, and you typed in all of the code correctly, it means that your text field is too small. If this is the case, unlock the layer containing the text field, make the field more wide or change the font size if it’s too big.

Now, let’s tell Flash if our movie has been loaded, to proceed to the content scene with the image in it.

14 After the last line of our current ActionScript code, add the following:

if (percentage >= 100) {

This piece of code tells Flash that if the percentage variable has a value greater or equal to 100 (i.e. 100% of our movie has been loaded), it must proceed to frame 3. Remember, our preloader scene has only two frames, so the 3rd frame is considered to be the 1st frame of the content scene. So, after 100% of our Flash movie has been loaded, the playhead will jump to the content scene. Test the movie once (CTRL+ENTER) and you’ll see the content, our image, appear immediately. Press the same keyboard shortcut again and there is only white screen. In order to correct this, modify the line

percentage = Math.round(movieLoaded/movieTotal*100);

so that it reads (the change is marked in bold)

percentage = Math.floor(movieLoaded/movieTotal*100);

The method floor tells flash to round the number between parenthesis to the lower value. That means, if the operation between parenthesis yields, let’s say, for example, 28.9, or 28.1 or 28.5 or 28.7 it will always be rounded to 28, NOT 29. Now why this functions and the method round does not, I don’t know. It just is so. Just so that you know, there is also a method ceil (Math.ceil), which rounds the number to the higher value.

That’s it for the basic preloader. Check out the other preloader tutorials to find out how to create a graphic preloader and other kinds of preloader tricks and variations.

Download the zipped source FLA file.

Be Sociable, Share!


Submit a comment

  • Sebastian G Jan 8, 2009 at 4:28 pm

    it works perfectly, you can also add some movie clip while the content is charged.

  • carlos Jan 10, 2009 at 12:03 am

    cool thanks, nice tuts, very concise and clear presentation.

  • getempmedia Feb 2, 2009 at 3:56 am

    very nice tutorial specially for beginner in flash.
    thanks it was a great tutorial

  • CR Feb 4, 2009 at 3:09 am

    Thank you. I was having a few issues finding the right way to add a preloader inside a .fla file. Was getting nervous and you nailed it.

    Thanks for sharing and wish you the best


  • emilio Apr 7, 2009 at 3:11 pm

    how can you activate the movie clip, or where to put it, coz I put it aside frame 1 and nothing happens, yes it loads and shows the percentage but the movie clip isn’t played

  • wah Apr 23, 2009 at 4:29 pm

    great tutorial. easy to follow. looking forward to the animated preloader tutorial.

  • Tany Jul 7, 2009 at 12:25 pm

    Excellent tutorials…Is it possible to print these out for reference? I’ve tried but unable to get the pictures printed…Please reply.

  • Kirin Aug 21, 2009 at 11:22 pm

    Cool it works! My website has multiple pages fill with flash content, but for now only 1 page needs a preloader of sort to let my visitors know it’s loading. So thanks!

  • Joanna Oct 30, 2009 at 2:58 pm

    Thanks sooo much for this tutorial. I have spent so much time practising various online tutorials to make a preloader and this is the only one that has actually worked for me.Phew!!

  • steve Nov 12, 2009 at 7:14 pm

    I love your precision. Congrats 4 the job!

  • Jason Dec 8, 2009 at 4:54 pm

    Am I crazy, or this tut utilizing ActionScript 1??

  • Luka Dec 9, 2009 at 8:47 am

    @Jason No, you’re not crazy. It IS AS 1.0 🙂 There are other tuts with preloaders using AS 2.0 and soon there will be preloaders with AS 3.0!

  • clara Dec 18, 2009 at 3:57 pm

    WOW!!!!! I finally get action script! thanks so much for explaining each line and what it does. It really helped allot

  • kabyhok Dec 30, 2009 at 11:42 pm

    I had a problem with the page it is preloading, that it was in the lower right hand corner, cutting off most of the page content. Is the registration for the preloader in the wrong area?

  • sarah Mar 10, 2010 at 11:43 pm

    Hi, thanks for the tutorial it was great.

    The only problem i am having is that the font i used does not show up on any other computer but mine where the font is installed. How do i still use my selected font and stop this from happening?

    Thank you 🙁

  • Luka Mar 11, 2010 at 7:40 am

    @Sarah: check out the spherical preloader lesson, this is explained in step 6.5.

  • Luka Mar 11, 2010 at 7:42 am

    …or better yet, the ultimate flash image gallery tutorial, step 3.12.

  • Robert Apr 28, 2010 at 10:04 am

    I just want to combine percentage preloader with preloader loading bar
    with siteLoader external swf. Can you teach me the code. Thanks

  • Luka Apr 28, 2010 at 11:35 am

    @Robert: Here it is: Complete preloader

  • Ben Jones Jun 26, 2010 at 12:16 am

    Great tutorial! It’s exactly what I was looking for, and so well-explained. Thank you so much.

  • Hammad Aug 8, 2010 at 12:33 pm

    thak u so much.. it .help me alot to start work on flash…

  • Jennifer Oct 11, 2010 at 9:21 pm

    Wow what a great tutorial! You completely explained every step and helped me to understand the action-script and the proper way to organize my scenes. Thanks for taking the time to write this tutorial.

You must log in to post a comment.