Smooth PreLoader

Page 1 of 1
James Drake
This user is yet to take control of their account and provide a biography. If you are the author of this article, please contact us via support AT actionscript DOT org.
View all articles by James DrakeDifficulty Level: Beginner
Download
preloader.fla (version MX)
Intro
This tutorial will show you how to make a smooth preloader for your site. This method does away with the old "ifFrameLoaded" method which led to jerky preloaders such as this one:
preloader2.swf
After you have finished this tutorial you will end up with something like this:
preloader.swf
Note
This tutorial is aimed at users with Flash MX but the ActionScript is identical for Flash 5 . If you wish to acheive this effect in a lower version of Flash it is likely that a lot of re-writing of the script will be necessary, and it may not even be possible. Descriptions of the user-interface within this tutorial are for Flash MX only.
Step by Step
- Open Flash and start a new project with dimension of 350 X 350, set the frame rate at whatever you want, I usually use 30 fps, set the background colour as whatever you like, I used white for this example.
- Name the top layer 'Actions' and make two keyframes on frames 1 and 2. Open the 'Actions' window (Window -> Actions) and select frame 1 in layer 'Actions'. Put the following code into the 'Actions' window:
stop();
Do the same for frame 2.
- Insert a new layer and name it 'Image', add two keyframes on frames 1 and 2. Import a photo into frame 2, keeping frame 1 empty. This is just to give the preloader some size to work on.
It should now look something like this:
- Insert a new layer and name it 'PreLoader', and add one keyframe to frame 1, don't put anything in frame 2. In frame 1 use the text tool to write 'Loading:'.
- Select this text box with the arrow tool and open the 'Properties' window (Window -> Properties). Make sure the top-left drop-down box says 'Static Text' and then change any other setting to how you like it.
- Select this text box with the arrow tool and go to Insert -> Convert to Symbol... Chose 'Behaviour: Movie Clip' and add the name 'PreLoader'. Hit 'OK'.
- Double-Click on your new 'PreLoader' movie clip. Name the one layer that is already there 'Static Text' and make three new layers, call them 'Actions', 'Percentage Text' and 'LoadBar'.
- Open the 'Actions' window (Window -> Actions) and select frame 1 in layer 'Actions'. Put the following code into the 'Actions' window:
stop();
- On frame 1 of the 'Percentage Text' layer use the Text Tool and drag a text box so it is next to the 'Loading:' text box you already have. Open the 'Properties' window (Window -> Properties), and make sure the top-left drop-down box says 'Dynamic Text'. To the right of the 'Properties' window there is a box labeled 'Var:', give it a value of 'percentage'. Next to that box there is a button labelled 'Character..', click it and then make the following settings. Out of the three options choose 'Only', then check the box that says 'Numerals (0-9)'. In the box at the bottom labelled 'And these characters:' input a value of '%'. Hit 'Done'.
- On frame 1 of the 'LoadBar' layer use the Rectangle Tool and draw a small square below the 'Loading:' text box. Select the arrow tool and position your mouse at the very edge of the square and double click. You should have now selected only the border of the square, not the whole thing. Hit 'Delete' on your keyboard.
- Select the square and open the 'Info' window (Window -> Info). In the box labelled 'W:' input a value of 1, and in the box labelled 'H:', make it a value of 10. Make sure the values in the 'X:' and 'Y:' boxes end in .0, ie, if you have 34.5 and 23.8, make the values 34 and 24.
- Select the small rectange you have now and go to Insert -> Convert to Symbol. Chose 'Behaviour: Movie Clip' and add the name 'LoadBar'. Hit 'OK'.
- Open the 'Properties' window (Window -> Properties), and give the box that says '<Instance Name>' a value of 'loadBar'.
It should now look something like this:
- Exit editing the PreLoader movie clip by double-clicking anywhere on the grey area around the movie, or clicking on the text 'Scene 1', this is just below the list of all the layers in the 'Timeline' window. You should now be back to editing the main timeline.
- Open the 'Actions' window (Window -> Actions) and select the 'Preloader' movie clip you have made. Put the following code into the 'Actions' window: onClipEvent (enterFrame) {
loading = _parent.getBytesLoaded();
total = _parent.getBytesTotal();
if (percent == undefined) percent = 0;
percent -= (percent-((loading/total)*100))*.25;
per = int(percent);
percentage = per+"%";
loadBar._width = per;
if (percent>99) {
_parent.gotoAndStop(2);
}
}
How does this ActionScript work?
- onClipEvent (enterFrame) {
This had strange wording for what it actually does. By the look of it it runs each time the movie clip enters a frame, but in fact the effect it actually has it just constantly running the code within it. Just like a loop, but indefinitely.
- loading = _parent.getBytesLoaded();
This gets the amout of bytes of the .swf file that have loaded and assigns it to a variable named 'loading'.
- total = _parent.getBytesTotal();
This gets the total size of the .swf file and assigns it to a variable named 'total'.
- percent -= (percent-((loading/total)*100))*.25;
Now, this is where the magic happens. You don't really need to understand the script, but what it effectivly does is add a 'lag' to the progress of the loading. For example, when he percent of the .swf file loaded goes from 10% to 30%, the old style of preloader would do the same, the loading bar would jump straight from 10% to 30%. This is what we want to get rid of with our preloader. This line of the script makes the loading bar smoothly move up from 10% to 30%, instead of jumping straight up.
- per = int(percent);
This takes the percent of the .swf file loaded and changes it to an integer for display in our text box. We don't want our preloader displaying something like 34.8484847449% do we?
- percentage = per+"%";
Remember the text box we gave a variable of 'percentage'? This line of script writes the percentage loaded to that text box, and adds a 'percent' sign on the end.
- loadBar._width = per;
Remember the small rectange we named 'loadBar'? This line of script increases the width to the percent of the .swf file loaded. So, when the percent loaded is 34%, the loading bar will be 34 pixels wide.
- if (percent>99) {
This says 'if the percent of the .swf file loaded is greater than 99% (ie. 100%) perform the following action'.
- _parent.gotoAndStop(2);
This is the action for the previous 'if' statement to execute. It moves the main timeline to frame 2, the frame containing your picture.
- }
Closes the 'if' statement.
- }
Closes the 'onClipEvent' function.
I'm stuck, how do I use this on my site?
Of course you won't use this EXACT preloader on your site. The purpose of this tutorial is so you can learn by example. If you have followed this through you should have an idea of which bits you can change to suite your site, and which bits you have to keep. But, if you're still stuck, help is at hand.
Within your preloader movie clip there are three things, your static 'Loading:' text box, your dynamic text box for the percentage value and the 'LoadBar' movie clip. Both text boxes can be customised to fit your site and additional graphics and such can be added. Make sure that the box you intend for the percentage to show up keeps it's 'percentage' variable intact.
Next is the LoadBar. I think the easiest way to do this is to position in and resize it (using the 'Info' window) as you want it to be when the movie has loaded 100%. When you have done that write down the current width of it, and then reduce the width to 1. When you exit back to the main timeline, select the 'preLoader' movie clip to edit it's actions. What you need to change is this line here:
loadBar._width = per;
Take the number you previously wrote down, and divide it by 100 (if it was 100 you don't need to do anything). So if your number was 134, you should now have a value of 1.34. Now change that line of code to this:
loadBar._width = per*1.34;
Obviously you will substitue your own values depending on your design.
That's about it. One other thing you may want to be aware of is this line:
_parent.gotoAndStop(2);
This is the action that the preloader performs when the .swf file has finished loading. You should make the beginning of your movie start on frame 2.
Credits
Thanks go to Craig Kroeger at miniml.com and JD Hooge at gridplane.com for help with the ActionScript in this preloader.
Spread The Word
Related Articles
26 Responses to "Smooth PreLoader" 
|
said this on 30 Apr 2007 1:29:03 PM CDT
this is a great tutorial,
|
|
said this on 31 May 2007 6:13:01 PM CDT
I used it in flash 8. It
http://eyecandy. Thanks for the tut! |
|
said this on 19 Jun 2007 10:51:12 AM CDT
great tutorial. breaks ev
|
|
said this on 05 Jul 2007 9:08:32 PM CDT
This tutorial is great wo
|
|
said this on 14 Jul 2007 4:41:09 PM CDT
Great tutorial. I always
if ( per |
|
said this on 16 Jul 2007 3:18:29 AM CDT
nice, sweet tutorial. Ju
|
|
said this on 18 Jul 2007 7:39:27 PM CDT
This is a very clear and
|
|
said this on 25 Jul 2007 5:58:12 PM CDT
Excellent step by step ex
|
|
said this on 30 Jul 2007 1:11:27 PM CDT
I use flash eight, and I&
|
|
said this on 03 Aug 2007 3:21:22 PM CDT
the items in your tutoria
|
|
said this on 08 Aug 2007 1:35:40 AM CDT
This is a great tutorial,
|
|
said this on 29 Aug 2007 9:59:14 PM CDT
Sir good day, i'm pra
here is th onClipEv Total Action h Respectfully you Ramon Gaerlan |
|
said this on 01 Jan 2009 11:39:40 AM CDT
I am getting the same err
|
|
said this on 30 Mar 2009 2:49:25 AM CDT
I will first explain the
The error is q SOLUTION: Click Pres |
|
said this on 14 Oct 2007 7:49:00 PM CDT
you're my idol man, i
|
|
said this on 16 Oct 2007 8:51:23 PM CDT
This is pretty simple and
|
|
said this on 20 Oct 2007 2:28:38 PM CDT
I can only agree with the
Only thing I didn percent |
|
said this on 22 Oct 2007 11:20:51 AM CDT
Excellent Pre-loader. Rea
|
|
said this on 05 Mar 2008 12:16:34 PM CDT
Can you increase the size
|
|
said this on 23 Apr 2008 9:45:20 AM CDT
This is such a wonderfull
Any ch |
|
said this on 07 Aug 2008 3:12:05 PM CDT
Excellent! just 1 thing t
|
|
said this on 31 Aug 2008 2:56:36 AM CDT
Thanks a lot! got me out
|
|
said this on 05 Jan 2009 2:40:35 PM CDT
For those who have troubl
You have to right c |
|
said this on 19 Jan 2009 7:48:00 PM CDT
Excellent! I was just wor
|
|
said this on 09 Apr 2009 7:19:09 AM CDT
this preloader not work i
|
|
said this on 16 Apr 2009 12:09:51 AM CDT
Hi, Great tutorial, howev
What am I doing wro Dele. |


Author/Admin)