Actionscript 3 Preloader

Hey,

So recently i've come to a problem, how do i create a preloader for my 
website if i only use Eclipse as development enviornment?
There is no main timeline per se, nor can i choose where to export my assets, 
so after some digging i came up with  this solution:

The main file:

And the preloader file:

Essentially what i'm doing here is instructing the compiler to add a frame
with myPreloader as a factory and calling my main file from it.
Should you have any questions feel free to ask, here is the swf.

16 Responses to “Actionscript 3 Preloader”

  1. cksachdev Says:

    Hi..
    I found it useful. I have one question. If I have to load different swf files on an event(suppose click) then on each class file I have to write this. [Frame(factoryClass="MyPreloader")]
    Or
    there is any other alternative to loading modules of a single project.

    I am also trying on mx.modules package as it provides API to load external modules.

  2. jawele Says:

    Hi there, thx for this, however I got an “TypeError: Error #1009: Cannot access a property or method of a null object reference.”

    when I launch it thru Flex 3, I guess that:

    [Frame(factoryClass="MyPreloader")]

    is only compatible with Eclipse?

    or am I missing something?

  3. jawele Says:

    Ok, I had the stage.NO_SCALE in my main class and it was making conflict,
    sorry for my n00bness.

    IT WORKS!!!

    thx so much ;)

  4. Idoru Says:
    @cksachdev - Well if you want to load external assets i would suggest you either load them in your main class, or just load the an swf with all the classes inside using the loader class. Then you can use the getDefinition to dinamically instantiate the classes, much like the code that instantiates your main class in MyPreloader.

    @Jawele - Glad you figured it out, you can still set all those properties in the preloader class.

  5. cksachdev Says:

    Hi Idoru,
    I wanted to use the Shell/Module pattern in ActionScript Project so there will be no modules loaded on Shell. Please suggest me how I can use mx.modules.Module class (if you have used) in ActionScript Project. Its worth a look.

  6. Idoru Says:
    Well the good thing about modules is that you can load them only when you need them and not all of them in the beginning.
    But the usage of mx.modules.Module is essentially the same as classes, meaning that if you want a specific class to be loaded before your main class you just instantiate it in the preloader.
    If you want to ‘cache’ them and instantiate later, you will have to have them imported in your main class.
    Another way of doing this is to use external SWF’s as libraries.

    Hope it helps.

  7. Lestaat Says:

    Hi all,
    is it possible to pass an argument to the factory?
    It’s just to make the code “BEAUTYFULL” i know, but i’d like to use the this way without copy MyFactory class in order to change ONLY the name of the mainclass
    :P
    I’ve try extending MyFactory class setting the name of mainclass on it and embedding [Frame(factoryClass="ExtensionOfMyFactoryClass")] but it seems that when getting definition by name in superclass look for mainclass in the same folder of the superclass instead of extended one.
    I know it’s a really stupid thing but i’d like to solve this problem :)
    Any ideas?

  8. Idoru Says:
    Hi Lestaat, as far as I know you can’t pass arguments in the meta tag, what you can do is pass parameters in the default-html so that you have your application class there as a variable, other than that you can create as many frames as you want using another frame tag with factoryClass or with extraClass, this should solve your problem. If you need any code example please do ask.

    Cheers.

  9. Dreaming in Flash » Blog Archive » Giving Flex Builder a Library (SWC) Says:

    [...] Have you ever wondered why Flex doesn’t have a nice library like Flash IDE? You’d love to use Flex but not having that nice little library there puts you off? Well here’s how we do it: First create your FLA file, then create all the items you want inside (they don’t need to be on stage just library will do), be sure to tick ‘export for actionscript’ and ‘export in firstframe’, also you can change the base class to something else that will add functionality to your movieclip. Then in export preferences, tick ‘export swc’. Ok so now we have a nice little SWC file, how do we get it into Flex? Easy Open project properties, go to Build Path > Library Path and add your SWC file. That’s it! Congratulations now you have auto complete goodness on your AS3 Flex Builder application Just keep in mind that these elements will be exported in the initial load of your website, so if you want to preload them be sure to use the preloader. [...]

  10. Chris S Says:

    Wow, thanks for the post! I’ve been searching everywhere for something like this, and everyone seems to implement preloaders via some awkard tweening and inline coding with CS3. It’s nice to see someone properly implementing it in real code.

  11. Chris S Says:

    When I try to compile this in Flash CS3, I get the runtime error:
    TypeError: Error #1007: Instantiation attempted on a non-constructor.
    at Main/::init()
    at Main$iinit()

    I added the Flex SDK to Flash CS3’s classpath. Is there anything else needed to get this to work in CS3, or is this code inherently specific to Flex?

  12. Chris S Says:

    When I tried to compile with mxmlc, I got the compile-time error:
    Main.as(11): col: 4: Error: Embed is only supported on classes and member variables.

  13. Chris S Says:

    In response to the “Embed is only supported on…” error, I figured that one out. Turns out I made the mistake of using Flash CS3’s “auto format” tool, which puts a “;” at the end of the [Embed...], which is of course a formatting error.

  14. Chris S Says:

    Btw, I’m pretty sure this code doesn’t update status, at all. onEnterFrame() will only be called once, not for every progress update, so your status bar will never get shown. I verified this by compiling an example with a 100MB image, and no status bar was ever shown. You should probably combine your code with the example at http://newmovieclip.wordpress.com/2007/03/14/preloader-in-flash-cs3-actionscript-30-way/

  15. Idoru Says:
    Hi Chris,
    I’m pretty sure everything works fine and it does update the status bar, but I have no idea of what will happen when you use this code in CS3, because CS3 and Flex have different ways of compiling the swf. This is essentially adding a frame to to the swf, which in Flex is a bit harder because of not having all that timeline stuff. I have no idea how to achieve the same functionality on CS3. Plus you can check that the onEnterframe does get called as it’s a listener, I think that you must have embbeded the image on the first frame and not on the second, hence causing the preloader not to show.

    But thank you for the comments and for the feedback, I will try and get a CS3 version working though.

  16. Chris S Says:

    Hi Idoru,

    Thanks for your feedback. Yeah, I’ve abandoned hope of getting it to work in CS3, since it doesn’t support the [Embde...] tag. I’m now focussing on getting it to work with mxmlc. Another problem I found was the [Frame...] tag also had a “;”, which blocked the preloader, but now that’s fixed.

    Unfortunately, I’m 100% certain your code as-is does not update the status. You can see this for yourself by using ExternalInterface to write to the percent to Firebug’s console.log. For example:

    ExternalInterface.call(”console.log”, ‘onenterframe loading:’+percent)

    You’ll see that only executes once. However, I did find how to make your code correctly update status, by adding these lines to the preloader’s constructor:

    this.loaderInfo.addEventListener(Event.INIT, onEnterFrame);
    this.loaderInfo.addEventListener(ProgressEvent.PROGRESS, onEnterFrame);
    this.loaderInfo.addEventListener(Event.COMPLETE, onEnterFrame);

    With these lines, Firebug shows the status percent being updated correctly. However, for some reason the graphics are not being refreshed. Even though the percent changes, all I see is the first status update where it’s only at about 30%.

    Can you think of any reason for this?

Leave a Reply