Minimal Preloader for AS3

Saw this post on preloaders today. I see a lot of similar examples. What I don’t like about any of them is the fact that they use the [Frame] metadata, which—as far as I can tell—requires you to use IFlexModuleFactory which means you have to bring in some minimal portion of the Flex SDK. That, I don’t like.

I’m not sure why Adobe dictated this in mxmlc, there’s no reason that [Frame] would require this to be implemented correctly. Unfortunately, they did, so we have to resort to using a compiler argument to get the result we want.

[Frame] metadata basically equates to the -frame compiler argument, but a little reversed. With [Frame] you specify which class should be placed in the frame previous to the class it decorates; with -frame, you specify which frame a class should appear on.

My standard preloader is named for the application’s SWF (so I don’t have to add any extra compiler arguments in Flex Builder), and once it finishes preloading it instantiates the actual application’s root class, which I normally name Application.

Because the application usually does some additional remote service calls and asset loading after the SWF completes, I don’t remove the preloader until it’s notified by the application that’s it’s ready. I use an event to do this so that there’s no hard-references either way.

This is an important point: whatever you reference in your preloader will be placed in the first frame of your SWF, so you have to be sure and not reference your application class (or really, any classes outside of the flash.* packages) statically. What you do is load them by name using getClassDefinition() once you’ve determined that the entire SWF has loaded.

Tags:

  • http://www.mrinalwadhwa.com/ Mrinal Wadhwa

    Hi Tony,

    That is perfect !
    I just tried out a quick example based on your code, works like a charm and no more mx classes in my SWF.
    Thank you for letting me know, I updated my post.

    Cheers,
    Mrinal

  • http://twitter.com/elliotrock Elliot Rock

    Troy,

    I find a direct scoping of a class's stage property to be a safer method, as in:

    this.stage.align = StageAlign.TOP_LEFT;
    this.stage.scaleMode = StageScaleMode.NO_SCALE;

    But that's cause I am a scoping freak especially with properties of the class.

    Overall the idea of a preloader is too be as light as possible and this does achieve it. It reminds me of the preloader I wrote for all the as2 template sites.

    Having an event to drive the end of the loading period is a good idea as you can included any other asset loading (setup XML etc..) in the loading phase.

    Thanks for pumping some sensible and simple script out!

  • troygilbert

    Curious, how do you find the direct scoping of a class's stage property to be safer? Considering that “this” is implied with all but local scopes, I'm not sure what problem could arise.

  • http://www.mrinalwadhwa.com/ Mrinal Wadhwa

    Troy, I'm embarrassed I called you with the wrong name, it was 5:30 am here when I wrote that comment :-/

  • sakri

    Hi,

    I must be missing something, but when I replace the “main application” with the preloader code, the rest of the application isn't compiled, resulting in a 1kb swf file.

    I looked into additional compileroptions, -include-classes looked the part, however, flexuilder nor intelliJ seem to recognize that option.

    How do you get the meat of the app to compile then?

    Thanks!

    Sakri

  • troygilbert

    Note the section of the post that describes using the “frame” compiler
    option to define you Application class.

  • sakri

    Thanks Troy,

    I was being an idiot with my compiler arguments…. This works:

    -frame 2 Application

    Thanks again!

  • troygilbert

    I've never used the extra arguments, just the frame argument like
    such: <tt>-frame 2 Application</tt>. My Application class is usually
    in the default package, so I don't include any package name, but your
    class name does need to be fully-qualified.

  • sakri

    Hi,

    I must be missing something, but when I replace the “main application” with the preloader code, the rest of the application isn't compiled, resulting in a 1kb swf file.

    I looked into additional compileroptions, -include-classes looked the part, however, flexuilder nor intelliJ seem to recognize that option.

    How do you get the meat of the app to compile then?

    Thanks!

    Sakri

  • troygilbert

    Note the section of the post that describes using the “frame” compiler
    option to define you Application class.

  • sakri

    Thanks Troy,

    I was being an idiot with my compiler arguments…. This works:

    -frame 2 Application

    Thanks again!

  • troygilbert

    I've never used the extra arguments, just the frame argument like
    such: <tt>-frame 2 Application</tt>. My Application class is usually
    in the default package, so I don't include any package name, but your
    class name does need to be fully-qualified.

  • http://twitter.com/stefanmihoc Stefan Mihoc

    Doesn’t work for me. How do I add the additional compiler argument in Flash CS4?