The Last Preloader You’ll Ever Need, Really

It turns out the preloader code in The Last Preloader You’ll Ever Need was not without its flaws. Today’s article presents an updated preloader that eliminates the added Flex framework fluff in the original preloader, and provides a helper class, SimulatedPreloader, that makes it super easy to test your preloader code.
Flawed Logic
The original preloader code used a bit-101 blog entry as its starting point for implementation. As it turns out, that was a bad move. It introduced a dependency on the Flex framework that added about 40k to the resulting swf produced. This updated preloader has no such dependency on the Flex framework, making it not only easier to setup, but also 40k lighter. The credit for this improvement has to go to Philippe at Flash Develop where he chastisied my original preloader article for introducing that dependency. Thanks Philippe!
The usage and gotchas from the original article pretty much apply across the board, but I will repeat them here just to remove any possible confusion over the changes.
Usage
The new preloader base class is simply called Preloader, this is to distinguish it from the previous implementation (ie: AbstractPreloader). You must subclass Preloader in order to create a meaningful preloader for your game. These are the steps required to get your preloader working:
Extend the provided Preloader class.
Override the following protected functions: beginLoading, updateLoading( percent ), endLoading. If your Main class is not called “Main”, then override the mainClassName function too.
Right-click the Preloader subclass and select “Always Compile” in Flash Develop. This is your new entry point.
Be mindful of gotchas.
That’s it, really.
So, here is my #1 and #2, a simple (and super ugly) % displayer:
public class MyPreloader extends Preloader
{
private var m_textfield : TextField;
public function MyPreloader()
{
}
override protected function beginLoading() : void
{
m_textfield = new TextField();
addChild( m_textfield );
m_textfield.text = "0%";
}
override protected function updateLoading( a_percent : Number ) : void
{
m_textfield.text = Math.round( a_percent * 100 ) + "%";
}
override protected function endLoading() : void
{
removeChild( m_textfield );
m_textfield = null;
}
}
Celebrate
You’re done! It’s really that easy. You should never need another preloader for your AS3 projects.
Sober Up
There are a couple gotchas as a result of this method of preloading that are not significant unless you forget about them, so let’s review them quick.
Gotcha 1
Don’t link or put anything “heavy” inside the preloader subclass. Your subclass of Preloader should be as light as possible. Anything that is linked or otherwise created in that class has to be loaded before your preloader can begin.
Gotcha 2
Your preloader subclass becomes and will remain the document root of the document. When Main has finished preloading it is added as a child of the preloader. The preloader will exist forever, so make sure you clean it up in endLoading.
Gotcha 3
Main’s contructor doesn’t have a reference to stage. Because Main is now a child of the preloader, it does not have a reference to the stage until after the constructor has completed. You should be familiar with dealing with these sorts of issues. If you need to, add a listener for the ADDED_TO_STAGE event and do your init code in that handler.
SimulatedPreloader
SimulatedPreloader is the new member of our Preloader family. As it turns out, it’s quite a pain in the ass to have to upload your Preloader every time you want to check if your preloader turned out right. Simply change your Preloader subclass to SimulatedPreloader and run it locally to simulate preloading in order to test your Preloader subclass. IMPORTANT: Once you are happy your preloader is bug free, do not forget to switch back to using the Preloader subclass, as the SimulatedPreloader only simulates preloading, it doesn’t actually do any preloading. It therefore will whine a whole bunch (tracing a big fat message and also throwing some debug text up in the corner), just as a reminder to switch back to the Preloader when you’re done testing.
Here’s an updated Preloader example demonstrating the new preloader outlined above. Source code is available at the Google Code gamepoetry project, under /projects/preloader_version_2.


bas
Works like a charm in your example code, but couldn’t get it to work for my own project until I found out that I needed to put ‘-frame start Main’ in ‘Additional Compiler Options’ for the project!
Keep up the good work, I love your blog.
October 14th, 2008 at 10:46 amPanayoti
Did you set the preloader as “Always Compile” in Flash Develop?
October 14th, 2008 at 11:02 ambas
Yes I did, but it wasn’t working until I noticed the line: option additional=”-frame start Main” in your FlashDevelop as3proj file.
Mine did not have this line and therefore never included the Main class in my swf. After including this line it worked ok.
October 14th, 2008 at 11:31 amPanayoti
Hmm.. That line was automatically added when I created the project. I’ll look into it in more detail and see if I can come up with an explanation.
October 14th, 2008 at 12:00 pmbas
If you create a fresh new AS3 Project in FlashDevelop, does it add “-frame start Main” ?
I’m using FD version 3.0.0 Beta 9. New projects are created with an empty “additional options” node.
October 14th, 2008 at 12:11 pmtrevorhartman
this is massively awesome. thanks. my only change was to do addChild( main ); instead of addChildAt( main, 0 );
October 14th, 2008 at 11:29 pmYAYitsAndrew
I’m using the latest flex3 sdk and I needed to add the -frame start Main option to my compiler options
November 12th, 2008 at 4:38 pm