Jesse lives and works in Melbourne Australia. He is the Cofounder and a Director of http://ActionScript.org. A Flash enthusiast, teacher, author, freelancer and speaker Jesse enjoys participating in the http://ActionScript.org community and the wider Flash scene when he has time. Because this is a preloader tutorial the example swf is 300kB to warrant preloading. To spare those of you who might be on a dialup I've not included it here. To see it in action click here.
You will all need the source however, so download it here.
Known Limitations:
Before we begin I will let you know that there are two limitations which I know of for this preloader.
This tutorial deals with a common question I see on forums all about the place. "I've got a movie which consists of a main body movie into which I load lots of little movies and I can't figure out how to preload them all!". Believe it or not, this was the 2nd question I myself asked when I started Actionscripting and I will be forever grateful to the guy who helped me out because he earned me a flipping great wad of cash :o) The solution he gave me, with a few changes, is that which I now bestow upon you dear reader, and it will work in Flash 4 and 5.
When you open up the main.fla from the source file I have provided you will notice 2 layers. The first, named 'Visual' is of no real relevance, it just provides so text while the content is loading, this is where you would insert your fancy little loading animation. The next layer called "Script" contains the important stuff so let's start there. The Script layer must contain one frame for each movie you intend to load. So if your movie loads 10 little movies into it, your script layer must have 10 frames. I also add another frame at the end with just a stop() action for good measure.Now let's examine the script. First the Mainf.fla, Frame 1.
[as]stop ();This script states:
- Stop playing
- Load the phase1.swf movie clip up on Level1.
Note that this script is the same for all the other frames, except we load a different number phase into a different level.
Now we need to look at the source of phase1.fla, so open it up. You will see it has it's own little preloader in the first 2 frames:
Frame 1:
[as]total_bytes = _root.getBytesTotal();Ignore the first 3 lines of this, they are simply my percentage loaded calculator from the Percentage Preloader tutorial. Line 4 is the standard preloader test to check if our last desired frame is loaded. In this case, it checks if Scene 2, Frame 1, in the phase1.swf clip is loaded. If so, it performs the actions within the curly brackets. Here's the trick: normally you would have a gotoAndPlay() action within those curly brackets to start your movie going now that it's preloaded, but we don't want to start it yet because the other's aren't loaded, so I've inserted a stop() instance. I've also added a Tell target goto command ( _level0.nextFrame(); ) , using the Flash 5 syntax, which sends our main.swf file to the next frame. Note that this is a gotoAndStop command, (nextFrame() is not a play command), as a gotoAndPlay command would cause debugging errors.
Frame 2:
[as]gotoAndPlay (1);[/as]Frame 2 loops back to Frame 1 until Frame 1 indicates the movie is completely loaded.
So now our first clip is loaded and our main movie has been moved forward one frame. Can you guess what's in the 2nd frame of our main movie? That's right, a command to load the second loadable clip. Once this one is loaded, it will send our main movie to Frame 3 ... I'm sure you've got the hang of the cycle by now. This is also why you should have one frame per loaded clip plus one additional frame at the end. This additional end frame provides a target for the _level0.nextFrame(); command of the final loaded clip.
That's it! If you need a tutorial which deals with one of the scenarios in the "Known Limitations" section, email [email:jessestratford@actionscript.org] me about it. If I get enough emails I'll make one :o) If you have any questions please post them on the forums rather than ask me via email.
PS - That X graphic is the same one I used in the other preloader tutorial. Copyrighted to me, Jesse, in case you're wondering. You can download it here if you like it.
| Jesse Stratford [email:jessestratford@actionscript.org] is the Co-Master of ActionScript.org and a freelance Flash developer and teacher. He is based in Australia and enjoys all things Flash. NB: If you have comments or feedback please feel free to email me, but please do not email me Flash questions; the forums are provided for that purpose and you will get a faster answer by posting you question there. |
If you have found this tutorial helpful, I hope that you will take 30 seconds to visit The Hunger Site where, with just one click you can make a free donation of food to a starving person in a third-world country. We do not benefit financially from this action; it is purely an act of charity. |
| This tutorial is protected by International Intellectual Property Rights laws and may not be reproduced or redistributed in full or part, without the prior written consent of the author. Unauthorized reproduction of this tutorial or its contents may result in prosecution. I've worked hard on this tutorial, please don't steal it. |