All this code was found in the mtasc list so the credits are for the mtasc list subscribers
The simplest scenario is taking a class and compiling it into a SWF:
mtasc -cp "C:\Documents and Settings\Aral Balkan\Local Settings\Application Data\Macromedia\Flash MX 2004\en\Configuration\Classes" -header:320:240:30 -swf sample.swf -main Application.as
This will create a new SWF with dimensions 320px by 240px and at a frame rate of 30fps called sample.swf. It will compile the Application class in the default package and inject it into this SWF (all in one step.)
The Application class must have a static main() method. This provides the entry point into the application.
Somewhere in your code you must instatiate it first for compiler purposes
in some init class
var foo = new com.package.className();
in some call class
var myInstance:instanceType = new (eval("com.package.className"))();
Or
var myInstance:instanceType = new com.package["className"]();
Or
var t = _global.com.package["className"]["methodName"];
An alternative is to simply include a reference to the class that you wish to dynamically create an instance of. This has the advantage that at runtime a new instance of your class is not created as a side affect.
var classReference:Function = com.package.className;
class ExampleMovie extends MovieClip {
public static var linkageId:String = "__Packages.com.asunit.example.ExampleMovie";
public static var classRef:Function = ExampleMovie;
public function ExampleMovie();
{
var mc:MovieClip = _root.createEmptyMovieClip( mcName, _root.getNextHighestDepth() );
mc.beginFill(0xff0000, 100);
mc.moveTo(0,0);
mc.lineTo(20,0);
mc.lineTo(20,20);
mc.lineTo(0,20);
mc.lineTo(0,0);
mc.endFill();
}
public static var serializable:Boolean = Object.registerClass(linkageId, classRef);
}
Question: do we need to add the -separate option?
NOTE : This explanation is obsolete, since mtasc 1.12 use this option by default and provide a -group switch to mimic this old behavior.
MTASC behaves differently than the Flash compiler. By default (and without telling you), Flash compiles each class inside a MovieClip. Here is the the initial article of the discover. By contrast, MTASC compiler can use the same technic or put all your classes in one big MovieClip. You can choose the option with a commandline switch. Let's talk of the pros and cons later…
The -separate option, once used, compiles your classes exactly the same way than Flash could have done. Example :
$ mtasc -separate -cp /MyClassPath Main.as -swf Main.swf
This commandline will produce a swf in which each classes are splitted in respective MovieClips.
Pros with the -separate option :
Cons :
But there is an issue that you must beware of. MTASC comes with another option : -keep. That commandline switch is usefull to keep existing classes in place and add new ones with MTASC. (The usual case is when you start a project in Flash and wants to accelerate your workflow with MTASC.)
If you combine -separate and -keep, MTASC can't distinguish between MovieClip classes that was produced by him. Than, as a result, you'll add to your swf all the new classes several times, as much as you compile ! This misbehavior is due to the freedom MTASC gives you to choose the way you compile your custom classes.
Fortunatly, there is a workaround, simply use :
$ mtasc -keep \
-separate \
-cp /MyClassPath \
Main.as \
-swf Main.swf \
-out Main_upadted.swf
Note the -out switch. This will take the original Main.swf (compiled first with Flash), let it untouched, and add all your new classes to Main_updated.swf.
As a side effect, if you use a versionning system, you could version once Main.swf as a base build.
— Eric Priou 2005/07/01 04:13
The swf format is compiled bytecode that will be analyzed later by the player. But there is a limitation.
To handle package structure, each class is compiled in an “if” statement. The opcode structure only support (today) 32k of compiled bytecode, which is quiet a lot by the way…
When you do not use -separate option with MTASC, you could reach this limit more easily (see To -separate or not to -separate for details).
If this limit is reached, MTASC will let you know. But this feature is really working since version 1.06. Beware to upgrade to the last one.
You can trace statements, objects, etc. by using the trace keyword:
trace("hello world");
Note:As of mtasc 1.10, trace has to be written in lowercase.
You then compile your actionscript with mtasc and add the -trace switch followed by an argument.
If you want to remove traces when compiling with mtasc, use:
-trace no
If you want to use your own trace class, use:
-trace MyClass.myTrace
Discussion