MTASC Applets

quickbuild.jpgMTASC, the open-source Actionscript compiler, has become an essential part of my daily routine. I used it for a long time with custom built BAT files that would inject compiled code into my library SWF's, but now I can do all of this with FlashDevelop. There are other editors out there, but FlashDevelop is my current IDE of choice, and it just so happens that it has a cute little feature I've not seen elsewhere. That feature is called "Quick MTASC Build", and I use it all the time to build quick test SWF's without ever having to open up the Flash authoring environment. I refer to these FLA-less SWF's as MTASC Applets.


The quick build button is the little red icon in between the syntax checker and the test movie button in the main FlashDevelop toolbar. When pressed, it looks for appropriate MTASC command line parameters in your primary comment block, and uses them to build a SWF out of the current class. It seems a little less than quick the first time you do it, but once you figure it out, it gets to be quite smooth. Here is my barebones example:

Actionscript:
  1. /**
  2. * This is a barebones MTASC Applet
  3. *
  4. * @mtasc -swf com/bumpslide/example/applets/MinimalApplet.swf -header 500:400:31:eeeedd -main
  5. */
  6. class com.bumpslide.example.applets.MinimalApplet
  7. {   
  8.     static function main( mc:MovieClip ) : Void {
  9.         mc.createTextField('message_txt', mc.getNextHighestDepth(), 10, 10, 500, 100);
  10.         mc.message_txt.text = "Hello, World";
  11.     }
  12. }

Because we put -main in the @mtasc command above, the static 'main' function will be called at startup, and a reference to our root timeline will be passed in as an argument. Here, we simply are creating a text field on the stage and saying hi.

Now, this is a good start and a useful reference, but this isn't much fun, and it's a bit of a pain to setup each time. For this reason, I've created a base class called MtascApplet that I reuse each time I want to create a simple test movie.

MtascApplet extends MovieClip and there is a simple bit of copy and paste that needs to happen each time you use it. That is, you need to paste in a copy of the static main method that turns the root timeline into an instance of the class at hand. Here is the code I normally start with:

Actionscript:
  1. import com.bumpslide.util.*;
  2. import com.bumpslide.core.MtascApplet;
  3.  
  4. /**
  5. *  Simple Applet Template Including FlashDevelop mtasc quick-build
  6. *
  7. *  Open this file in FlashDevelop and hit the "Quick MTASC Build" button.
  8. *  Use this as a base for creating new applets.
  9. *
  10. *  @mtasc -swf com/bumpslide/example/applets/SimpleApplet.swf -header 500:400:31:eeeedd -main
  11. */
  12.  
  13. class com.bumpslide.example.applets.SimpleApplet extends MtascApplet
  14. {   
  15.     // gives us right-click to view source link
  16.     var sourceUrl : String = "SimpleApplet.as";
  17.    
  18.     // onLoad show message text
  19.     private function onLoad() {  
  20.         message("Hello, Flash Nerds.");  
  21.     }
  22.    
  23.     // trace StageProxy dimensions on resize
  24.     function onStageResize() {
  25.         Debug.trace( "Stage size: "+stage.width+'x'+stage.height);
  26.     }
  27.    
  28.     // All applets must contain a custom version of this method
  29.     // Just change 'SimpleApplet' to whatever your class name is
  30.     static function main(root_mc:MovieClip) : Void {           
  31.         ClassUtil.applyClassToObj( SimpleApplet, root_mc );  
  32.     }   
  33. }

This is not gorgeous either, but it is a start of a framework for creating simple test movies. I use it frequently, and there are a couple of samples along with all of this code in my Bumpslide AS2 Library.

Quick Tip: The path that gets passed to your -swf mtasc parameter is relative to your FlashDevelop class path. Your classpath is also the base url for all relative paths referenced inside your SWF. However, when you run the SWF on it's own, the base path is the same directory as the SWF. This is just something to keep in mind when you wonder why your image loading test movie isn't loading that image.

Examples: (right click on running swf's to view source)
Simple Applet
Spiral Trig Test
Springy Box with FTween

The last one is the most fun. It makes use of my target-based tweening class, FTween. But, that's a subject for another post.

Leave a Reply