Injection Swf Class hook

Also known as:

  • keeping your sources out of the injection swf
  • decoupling your main class from your injection swf

If you are developing your actionscript 2 apps in FlashDevelop, in a lot of cases this means you are using an injection swf.

This injection swf contains all your assets. There are two options to start your program once it’s loaded:

  • tell MTASC to execute your main class once all classes are initialized
  • call your main class method from the swf once everything is loaded

Note that these two points might not be the same. I use a simple fla template which contains a preloader etc, and has its classes exported at frame 20 and its main application function called at frame 40. Although you might argue that an external preloader should have been used and the main fla should only have been 1 frame, that offers new problems, and anyway that is not what this post is about.

Telling MTASC to execute the main class once all classes are initialized will cause initialization to occur at frame 20, using a call on frame 40 of the timeline will cause the application to be initialized at… yes frame 40.

Another difference is that using the MTASC main class option, causes your class file to be excluded during compilation of the injection swf (which is good), using a class hook causes them to be compiled in the injection swf (which is bad).

When the classes of your application are already compiled in the injection SWF, you will run into problems when refactoring the classes, and forget to recompile the injection swf. If you use the MTASC keep flag, all your classes will be removed, which is probably not what you want if you are using v2 components in the SWF.

So the simplest option is not to use:
com.mydomain.client.MainApplication.main (this);

But to use: (this);

When you tell FlashDevelop to include your main class, the class will be injected in the injection swf during compilation, so (this); will call an existing method and your swf will run just fine. On the other hand (this); will not cause any application classes to be included in your injection swf during export, which is good too.

So almost there, the only thing that’s not so good about this is that the injection swf still has knowledge about your application’s main class, since it’s right there in the statement.

To fix that you can replace: (this);



and include this code in your main class:

private static var _instance:MainClass = null;
public static var _installHookTrigger:Boolean = _installHook();
private static function _installHook():Boolean {
  _global.hook = function () {
   MainClass.main.apply (MainClass, arguments);
  return true;

 public static function main (pParent:MovieClip):Void {
  _instance = new MainClass(pParent);

Just compiling the main class into your swf will cause the _global.hook to be set, and calling it will cause your MainClass’s main function to be called.

Spiffy ain’t it 🙂 ?

Print Friendly, PDF & Email
0 replies

Leave a Reply

Want to join the discussion?
Feel free to contribute!

Leave a Reply

Your email address will not be published. Required fields are marked *