Flash Explained

Learn Flash easily.

Making the complete internal preloader in Flash 8, with a loading bar and mathematical preloaders – part 3 of 3

This is the final part of the lesson on building a complete preloader with a loading bar and numerical indicators of preloaded Flash website contents.

The ActionScript code explained

The first thing that you tell Flash to do is to stop the movie from going on. In this way, the playhead doesn’t go forward. If it went forward, your site visitor(s) would see nothing, because the SWF movie hasn’t loaded yet. That’s why you have to stop it.

stop();

The next line,

loadingBar._xscale = 1;

sets the _xscale property of the loadingBar movie clip to 1. What is this property? First, this is a percentage value. So, 1 in the line above means actually 1%.

By default, when a movie clip hasn’t been manipulated via ActionScript, its _xscale property is set to 100%. Your loading bar has a width of 150 pixels (if you set it to this width, like I suggested in this tutorial). If you make its _xscale property equal 50, it means that its width will become reduced to 50% of its original value, therefore 75 pixels.

The same object shown with different _xscale values.

You have put this value for your loading bar to 1, because in the moment before your movie starts preloading, you want the bar to be at the smallest possible value.

Now comes a setInterval method that calls a function in regular periods. To explain it, I first need to explain you what a function is.

var loadingCall:Number = setInterval(preloadSite, 50);

A function is a set of ActionScript commands that are grouped together. Ok, but what does that exactly mean?

Imagine this: you are working in a fruit juice factory. There is a computer that tells the juice mixers what kind of juice must be made and poured in certain bottles. So you have different functions programmed inside this computer, for different kinds of juices. Let’s say you want to make a tropical juice (yum!). You would have a function in that computer that would tell it to mix different juices together, to have the tropical mix in the end. In ActionScript, this would be something like this (this is just a fictional example):

function tropicalJuice():Void {
orangeJuice = 30%;
mangoJuice = 30%;
pineappleJuice = 40%;
}

Ok. So that computer function would produce the tropical juice. But would it run by itself? No. Someone who operates the computer would have to press a button in order for the juice production to begin.

The same is with an ActionScript function: it just sits there and does absolutely nothing unless you call it and make it execute its code!

Doesn’t matter if your movie is playing or if it is stopped. The function won’t run unless you tell it to. So, how to make it execute? To run a function, you must call it. A simple function call looks like this:

tropicalJuice();

And that’s it. You just write the function’s name followed by parentheses and a semicolon and it runs every command that is placed inside its curly braces – { and }.

But it is run just once. After doing that, it is again just waiting and doing nothing until you call it again. With your preloader that would be inconvenient. The function must check how much of your SWF has been loaded continuously, if you wish your preloader to work.

So here’s a nice ActionScript gizmo that comes in handy: a setInterval method. This method (a command is called a method in programming jargon) can call a function at regular time intervals. This method must be placed in a variable, hence the line

var loadingCall:Number = setInterval(preloadSite, 50);

So, the first part, var loadingCall:Number tells Flash to create a variable (var), name it loadingCall and make it a Number kind of variable.

I could have called it callMyFunction or runPreloading, I just chose loadingCall because it sounded good to me. The Number keyword tells Flash to make this variable a, well, Number kind of variable. This means that this variable isn’t one which will hold a text or any other kind of value, but exclusively a numerical one.

On the right side of the equals sign, there’s what the loadingCall variable will hold inside it. The setInterval method has two values inside its parentheses: the first one is the name of the function you want to call (preloadSite in this case) and the second one is the time interval at which the function will be called regularly.

This time interval is expressed in milliseconds. If you wanted your function to be called every second, you would have written 1000. Since this is too slow a check for a preloader, 50 is a better value. It means the function will be called every 0.05 seconds, which is pretty often and pretty fine.

So, as soon as Flash reads that line of ActionScript, it will immediately start calling the function mentioned inside it, at the designated time intervals.

Now comes the function (I will explain its contents later):

function preloadSite():Void {
…ActionScript commands go in here…
}

The function’s first line defines it and gives it a name – function preloadSite. After the parentheses and the colon comes the keyword Void. This keyword tells Flash that this function does not return any value.

There are functions that return a value, like a sum of two numbers for example, but this one you’re making returns nothing. And you must tell that Flash too. Yep, this is how ActionScript 2.0 works. Everything is precisely defined, that’s why ActionScript is a strictly typed programming language.

Let’s see what’s inside it. The first two lines define two variables, which are both numerical:

var siteLoaded:Number = _root.getBytesLoaded();
var siteTotal:Number = _root.getBytesTotal();

The first, siteLoaded, will hold the number of bytes loaded so far. The keyword _root means the main timeline – the SWF movie itself. Therefore, _root.getBytesLoaded means “see and fetch the number of bytes of this SWF flash file that have been loaded up to this point”. And then this value is put inside the siteLoaded variable.

The siteTotal variable is similar to the one preceding it: it does the same, except that it holds the total file size (in bytes, of course) of the SWF movie, that has to be loaded.

The percentage variable,

var percentage:Number = Math.round(siteLoaded/siteTotal*100);

holds the resulting value of the operation on the right side of the equals sign. There is the number of bytes loaded so far (siteLoaded) divided by the total number of bytes (siteTotal) and multiplied by 100. This is the basic percentage formula in maths – divide part by whole, multiply by 100 and you get the percentage.

And there’s the Math.round method which rounds the resulting number between the parentheses. I recommend that you use it because you certainly don’t want something like 24.6012837% appearing in your text field. This may look cool or sci-fi like, but in reality this just confuses the user because it is harder to read than a round number (24%).

Now that percentage value gets used in a most interesting and cool way:

loadingBar._xscale = percentage;

Remember loadingBar? It is the Instance name of the loading bar movie clip – the graphical element of your preloader. So by telling Flash that loadingBar‘s _xscale property should equal percentage, you make it stretch as the movie loads.

If the movie has loaded up to, say, 50% by now, that information is stored in the percentage variable and then applied to the loadingBar movie clip. So once the movie has fully loaded (100%), this bar will come to its final size – 100%.

Each time the function is called (0.05 seconds), the percentage value gets updated, and the loading bar stretches together with it. Now ain’t ActionScript awesome? You can do anything you want with it. I love Flash!

Onwards. The following piece of code defines what is going to be displayed in the percentDisplay text field, which is itself situated in the movie clip called percentClip.

percentClip.percentDisplay.text = percentage + "%";

To help you remember where this text field is, here’s a little visual clue:

The movie clip with the arrow and a textfield.

If you just wrote

percentDisplay.text

this wouldn’t produce any result at all, because Flash would look for that textfield on the main (_root) timeline. Since this textfield is in a movie clip, you have to write

percentClip.percentDisplay.text

And the text that is going to be shown inside it is the value of percentage variable, plus a little percentage sign (%) after it. So, when the loading of your site has reached 34% for example, Flash joins the number 34 with the % sign and displays it.

Now comes the part where the aforementioned clip (the one on the picture above, with the arrow and the textfield) moves as the loading bar progresses.

percentClip._x = loadingBar._x + loadingBar._width;

The clip’s horizontal position (_x) is calculated by making a sum of the loadingBar movie clip’s horizontal position (loadingBar._x) and its current width (loadingBar._width). Like this:

Horizontal positions of movie clips.

In this way, the percentClip is always placed on the right edge of the loading bar.

This one is easy: it displays some information in the text field below the loading bar. The text between quotation marks is displayed literally, as written inside the ActionScript code, and the siteLoaded and siteTotal are variables defined at the beginning of the function, and their respective values are displayed. This text fields tells the user how many bytes have been loaded so far of the total bytes the site has.

bytesDisplay.text = "loaded " + siteLoaded + " of " + siteTotal + " bytes";

The conditional if statement runs what’s inside its curly braces only if it gets to be true.

if (siteLoaded >= siteTotal) {
clearInterval(loadingCall);
gotoAndStop(5);
}

Translated into English, this means that if the number of loaded bytes (siteLoaded) is equal or greater then the total number of bytes of the movie that is being loaded (siteTotal), the two lines of code which follow get executed.

clearInterval(loadingCall);

The loadingCall interval that used to call the function every 0.05 seconds is stopped with the clearInterval method. Why? Because you don’t need this anymore. All of your movie has been loaded – it can leave the preloader and proceed onto the frame where the content lies:

gotoAndStop(5);

And that’s it! Whew!

You learned a lot of ActionScript, which is excellent. Keep on going like that! If you are interested in different kinds of preloaders, like the ones with a mask, or that replace your mouse, check out my other tutorials on preloaders.

And if you need a preloader that works for Flash CS3, make sure to check out the excellent ActionScript 3.0 preloader from Kirupa.com.

Download the zipped source FLA file for this lesson.

You can also leave a comment.