As promised in my last blog entry, heres my version of an AS3 preloader. It's the second time i've typed this out, as the first time i had lost my cookies while messing around in another firefox window and when i came to post this entry it just said access denied and it had all gone! Lovely! Anyway the main points of my preloader are
- MochiAds & MochiBot already integrated
- Simple domain site locking implemented
- Nice glossy style progress bar
- Reusable code, not integrated heavily into a project
- Easy to add to existing AS3 project
- Pure AS3 classes
- Works with FlashDevelop or Flex Builder
- Minimal Delays, fairly small size
And most of all i'm giving all the source code away, not just snippets :)
It turns out that preloaders in Actionscript 3.0 are a bit problematic outside of CS3 (and even inside, judging by some of the forum posts i've read). It looks like there are two main ways of creating a preloader in AS3, using 2 slightly different undocumented features of the flex compiler (mxmlc). The first uses an Embed tag to specify a preloader factory class - you can read more about this method on Keith Peter's BIT-101 blog. The other method i discovered was on Lars Gerckens nulldesign blog. I've used the second method, as the first method adds a small overhead of some flex classes to the preloader swf (you can prevent most of these from being added with some xml, but this seems a bit messy to me). The method i've used allows you to specify the "Main" class to be loaded in another frame with the command line option "-frame two Main". This lets the preloader in frame 1 load and execute as frame 2 (the main part of the project) loads.
The preloader has both MochiAds & MochiBot (although you can easily disable either or both of them if you wish) and has optional simplistic domain locking. Obviously with AS3 decompilers available, you will want to add your own protection methods, but for simple demos this should be enough to stop the basic site scrapers / leechers (assuming you don't allow hotlinking on your server for swf files). The preloader should easily add on to any AS3 project for FlashDevelop or Flex Builder - i do not own CS3, so can't make a special version for that, if someone would like to i'd be happy to give them credit and host the example file/details for others to use. It shouldn't be that different and almost everything should be usable for CS3.
Check out the example by clicking on the image below. The main swf is simply a large photo. You may notice if you refresh too often that it sometimes skips the advert, this is normal and if you wait 30 seconds then refresh it should come back.
The progress bar may complete before the ad is even shown - this is not a problem, sometimes it takes a while for the advert to appear. The preloader will wait for the advert to finish then continue as normal. When the swf is cached, you should notice it loads a lot quicker the next time around. If you have any problems please leave a comment and let me know what happened - i haven't used this for a game yet, but will be doing soon. If i find any bugs, i will come back and update this loader when i have some time. Remember that this source code is supplied "AS IS", and there is no warranty or promise that it does anything at all, or is bug free - if you use this code you do so at your own risk. Hopefully someone will find this useful and save them a lot of messing around trying to get a preloader to work.
It's taken a while to get this correct, if you alter the preloader files, be careful not to reference the Main class directly, as this totally removes the preloading ability. Also there are other potential issues. If you have the main class init directly following a NextFrame() call, in certain situations you can get a long delay which is very noticable. To use the preloader, you need to create a new PreloaderFactory class that extends my preloader so that you can pass in your MochiAd & MochiBot ID, and also setup an optional list of allowed domains for the sitelocking code. The PreloaderFactory class should be set to always compile and be the document class of the project. The "Main" class should be the entry point to the real program (not the preloader), and where the main program is setup. This should always be called "Main" and you need to remember that the stage field is not correct when the constructor is called. If you need to access the stage during setup, you need to hook the ADDED_TO_STAGE event and then perform your init functions in that handler. You then need to make sure your passing "-frame two Main" to the mxmlc compiler (under compiler options in FlashDevelop / project properties). See the provided project zip for more information.
You can get the project in a zip here or you can download / browse the main source files here - PreloaderFactory.as Main.as Preloader.as ADInjector.as ProgressBar.as. The MochiAds & MochiBot classes are included in the zip. The Mochi files were taken off the Mochi forums and should probably be treated as beta code, if you find that there are updated versions for AS3 then you should be able to change them easily. The only change i made was to prevent the MochiBot class reporting unhandled IO errors.
Any comments or suggestions? Hopefully this will save a few people some time on trying to get a usefull preloader to work! Let me know if you use it on anything so i can have a look, or if you encounter any problems with it!

Tue, 05/20/2008 - 22:32
Very helpful. Thank you!
Wed, 04/09/2008 - 20:49
Third edit lucky ;). The only time i can get the loader bar to fail is when you press enter on the address bar when pointing directly at the swf or clicking a link to the swf directly. If you delete temporary internet files and hit refresh, or back/forward then it works fine. If you click on the address bar and press enter or click on a link to the swf directly it first downloads the whole swf then shows it which means it's already all cached and theres no loading. This doesn't appear to happen if you embed it in a htm page. Can you confirm this is the same problem? It looks like a IE "feature" where it downloads the whole file because it's not a webpage. Can you confirm this is whats happening? Theres a page for my loader test but i didn't use it because i haven't figure out how to get rid of the silly "press space" ie message that sits over the flash. http://www.adventuresinactionscript.com/sites/default/files/blog/actions...
BTW, I've seen your blog - but couldn't leave a comment or contact you, as i thought i had the same gzip problem with my server that you had, but it turned out to be a silly mistake i'd made in .htaccess. I upgraded to Drupal 6 and everything broke - been trying to fix it when i get a few moments, but i'm no expert in php so it's slow going. I think the problem was 2 empty lines at the bottom of template.php? Maybe it's ok now? Maybe :)
Wed, 04/09/2008 - 18:17
Let me know if you can repeat this behavior -- I've spent the last couple weeks obsessing about this and I'd love to hammer out a solution.
also, looks like you have some bad output in the _submit or _validate phase of your comment posting form -- throwing warnings.
er, actually it looks like there's a template.php mod on line 9 that's throwing output too early, but i could be wrong.
Wed, 04/09/2008 - 18:09
I'm looking at building one of these myself, and was interested to see if your version bugged out with IE7 like my version does. It does: I get no load bar on IE7 when I load your demo (a similar bug happens with my loader).
Are you experiencing this as well? I'm wondering if this is a browser-config issue (i have FlashGot installed, which I'm going to remove from IE7 and retest -- otherwise it's stock), or if there's some bizarre thing with IE7 where it can't handle this (unlikely, i guess).
Thanks for making this available! I'll let you know what I find out.
Fri, 04/04/2008 - 05:29
Really nice, I'd like to see it in use.
Keep the good things coming. Since I'm not developing AS3 - yet, won't I be able to use it right away. But when time comes, I will!
Post new comment