Using AsWing/as3 with Haxe
This simple tutorial explains how to use the powerfull AsWing GUI framework within Haxe
Prepare the Extern Class Tree
- Download latest version af aswing/as3 here
- Extract it in a folder.
- Go to the AsWing\bin directory, and rename the file AsWing.swc to AsWing.zip
- Unzip this file with your favorite zip utility
Now you have two files: catalog.xml and library.swf. It is the library.swf that is important for us.
- Start up the command prompt, and go to the directory where library.swf resides.
- Execute:
haxe --gen-hx-classes library.swf
It will genererate all the extern haxe classes in a subdir “hxclasses” needed to use aswing in haxe.
Please note : some generated extern definitions need to be updated, typically where the equivalent AS3 method uses the '...' opertor in the parameter list. (addRow in org.aswing.ext.FormRow is an example). Whenever the compiler complains about one of these, substitute '...' by a number (however many you need) of optional parameters.
Here is an example:
//AS3: addRow(...columns):FormRow{ //haXe: addRow(?p1 : Dynamic, ?p2 : Dynamic, ?p3 : Dynamic, ?p4 :Dynamic, ?p5 : Dynamic):FormRow{
Update May 24, 2009 : Using haXe 2.0.3 on Windows XP, no issues found concerning remaining '...' operators. YMMV
Setup a Test Project
Now let's start a new haxe project for testing.
- Create a new project directory, and copy the complete generated tree (“org” directory) in here.
- Copy the library.swf also in your project directory. This library will be compiled into your final movie.
Create a file Hello.hx:
import org.aswing.JButton; import org.aswing.JFrame; import org.aswing.geom.IntDimension; import flash.Lib; import flash.display.Sprite; import flash.display.StageScaleMode; class Hello extends Sprite { public function new() { super(); //create a frame var frame : JFrame = new JFrame( this, "HelloApp" ); frame.setSize(new IntDimension( 200, 120 ) ); //create a button var button:JButton = new JButton("Hello from AsWing in Haxe!"); //add the button to the frame frame.getContentPane().append(button); //add the frame to the stage Lib.current.stage.addChild(this); this.stage.scaleMode = StageScaleMode.NO_SCALE; //show it! frame.show(); } public static function main() { new Hello(); } }
Create a file Hello.hxml:
-swf Hello.swf -swf-header 600:400:21:ffffff -main Hello -swf-lib library.swf -swf-version 9
Now let's compile it by executing:
>haxe Hello.hxml
When everything went fine, you should have a flash file Hello.swf displaying a frame with a button in it, saying Hello!
Notes
I had to use haxe version 1.19, the older one didn't work. There was an error generating the extern tree.