Skip to content

AS3 Application Preloading – translated

2008 April 20

Juten Tach,

this is an article, i wrote last year. It was in german. Since i switched the language to english, i thought i translate this article to english as well. Here it goes:

My colleague Lars described in his article, how to use the [Frame] Tag together with a Factory class in order to preload your Main application by spreading your classes on different frames. Since it was a bit of work to do to accomplish that i felt i give it a try and seach for another solution that might be easier and i found a compiler-option based solution.

As an example, the following two classes are given:

The Main class:

package {
   import flash.display.DisplayObject;
   import flash.display.MovieClip;
   import flash.events.Event;
   import flash.utils.getDefinitionByName;

   public class Test extends MovieClip {
      public function Test() {
         stop();
         addEventListener(
            Event.ENTER_FRAME,
            onEnterFrame
         );
      }

      public function onEnterFrame(event:Event):void {
         if(framesLoaded == totalFrames) {
            removeEventListener(
               Event.ENTER_FRAME,
               onEnterFrame
            );
            nextFrame();
            init();
         }else {
            // Show loading stuff here.
         }
      }

      private function init():void {
         var mainClass:Class = Class(
            getDefinitionByName("Demo")
         );
         if(mainClass) {
            var app:Object = new mainClass();
            addChild(app as DisplayObject);
         }
      }
   }
}

And secondly, the actual application class, which should come to play in frame two:

package {
   import flash.display.Bitmap;
   import flash.display.Sprite;

   public class Demo extends Sprite {
      [Embed(source="somepicture.jpg")]
      private var picture:Class;
      public function Demo() {
         init();
      }

      private function init():void {
         trace("Hello Demo");
         var asset:Bitmap = new picture();
         addChild(asset);
      }
   }
}

But the most important part comes now, the following compiler option:

-frame two Demo

That simply puts the class Demo in a second frame called “two”. You can use the “-frame” compiler option as many times as you want for adding even more frames. The big advantage here is, that you don’t get these nasty flex classes in your application, that you don’t want. Everything’s nice and slender.

Unfortunately though, the Embed Tag in my class Demo still causes the mxmlc to compile a bunch of flex classes and interfaces into my app. Seems, that you cannot really do something about that, which is sad, because i don’t see, why i need a flex bitmap object, when a normal Bitmap Object would have been good enough.

Share this:
  • email
  • Twitter
  • del.icio.us
  • Facebook
  • Google Bookmarks
  • Posterous
  • Digg
  • Technorati
  • Slashdot
  • Suggest to Techmeme via Twitter
  • StumbleUpon
  • Reddit
  • Netvibes
  • LinkedIn
  • Mixx
4 Responses leave one →
  1. May 28, 2008

    Great tip, thanks for sharing it. I had a small issue where the compiler couldn’t find my Demo class, but it turned out I just needed to use the full name including package.
    If I put my Demo class in a package called com.example I needed to write getDefinitionByName(“com.example.Demo”) in my code and “-frame two com.example.Demo” in my compiler settings.
    Thanks again!

  2. May 11, 2009

    This is great – would you mind if I base our swf9 preloader code on this? We need something for the swf9 runtime for OpenLaszlo. I didn’t see a copyright or other licensing information, so I thought I’d ask…

  3. May 12, 2009

    Hi Max,
    no problem at all. This was meant to be shared and used by others. You can use it.

Trackbacks and Pingbacks

  1. AS3 Only Preloaders | xperiments.es

Leave a Reply

Note: You can use basic XHTML in your comments. Your email address will never be published.

Subscribe to this comment feed via RSS