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.

AS3 Preloader Example

     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!