-
Notifications
You must be signed in to change notification settings - Fork 78
Using SWC Files With AS3 And FlashDevelop
##ActionScript 3.0 support
The Haxe library comes with pre-compiled SWC files for supporting projects written in the ActionScript 3.0 language.
##Linking the SWC file
Linking those SWC files is very simple. For example, if you are using FlashDevelop, just copy the SWC file to the project folder, right-click the file and select "Add To Library" from the context menu:
The same applies to FDT, FlexBuilder etc. If you are using Adobe Flash CS4 Professional (CS3 is not supported): From the main menu choose File->Publish Settings... to open the "Publish Settings" window. Here, check the "Export SWC" option, make sure the script language is set to ActionScript 3.0 and click on the "Settings..." button to open the "Advanced ActionScript 3.0 Settings" window. Click the "Library path" tab, and then the "Browse to SWC file" button to add the SWC file:
There is always a SWC file called xxx_
debug.swc and xxx_
release.swc, where xxx is the name of the project. The debug SWC should be using during development and debugging, since it provides detailed stack-traces and validates user input. When the project is ready for deployment, compile it against the release swc file providing a significant performance improvement.
##Using the SWC file ###Initialization
Once the library is linked, you can start writing code. The main class should have a call to haxe.init(this)
to initialize Haxe specific things (not required for ds swc files). The following example creates a 3x3 two-dimensional array filled with zeros:
package {
import de.polygonal.ds.Array2;
import flash.display.MovieClip;
public class Main extends MovieClip {
public function Main() {
haxe.init(this);
var a:Array2 = new Array2(10, 10);
a.fill(0);
trace(a);
}
}
}
###Public and private
To ensure cross-platform compatibility and because Haxe has some language features that are not available in AS3, all methods and properties become public at the byte code level. This obfuscates the API of the SWC library, since all methods/properties are now public and listed in auto-completion. But since I'm always adding an underscore prefix _
to all private properties/methods it should be clear which of them are safe to use.
###Passing anonymous functions
In Haxe, an anonymous function is defined like this:
public function walk(process:T->Int->Int->T):Void {
...
}
This means the process function needs 3 arguments - an element of type T and two integers - and returns another element of type T. In AS3 this translates to:
//T is a string
var a:Array2 = new Array2(3, 3);
a.assign("");
var process:Function = function(val:String, x:int, y:int):String {
return x + "." + y;
}
a.walk(process);
##Generating AS3 source files
If you want to compile the library with the adobe compiler you can generate Actionscript 3.0 source files (.as) from the Haxe source files (.hx). This is not recommended since the generated code will run much slower! If you still want to try it out, run the Ant built file as3.xml from the build directory. If you don't know how to install Ant, take a look here: [InstallingApacheAnt]. If Ant is correctly installed, open a command prompt and type:
$ cd [directory containing as3.xml]
$ ant -f as3.xml
The source files are now stored in a directory called as3_debug and as3_release next to as3.xml.