Overview
This tutorial will show you how to access objects from an SWF file using Haxe and FlashDevelop. By following these steps you will be able to compile ActionScript 3 code into a SWC, extract the SWF file from the SWC, embed the SWF file in your Haxe project and access the ActionScript 3 objects.
Prerequisites
For this tutorial I am going to be embedding this AS3 SWF Profiler written by Shane McCartney. Note that I will be using the version downloaded from GoogleCode it has a different package than the one on his blog.
Compiling a SWC
To compile a SWC from ActionScript soure we need to use the compc component compiler that came with the Flex3 SDK. It can be found in the bin directory under your Flex3 SDK root dir. For example “C:\flexsdk\bin\compc.exe”
To run the compiler I found it easiest to create a config file and tell compc that to use it to work out what to compile. Here is an example config with comments explaining what each element does.
Then simply call compc from the command line and tell it to use this config file.
For example: ”C:\flexsdk\bin\compc.exe” -load-config+=config.xml
If all goes well you will end up with a new file called SWFProfiler.swc
Generate Haxe classes
After compiling our SWC file we need to generate some Haxe classes so that we can reference these object from our Haxe project. This is pretty easy as the Haxe compiler will help do this for us but first we need to extract the SWF file from the SWC file.
Take the SWFProfiler.swc and rename it to SWFProfiler.zip
Open this zip file and extract the library.swf file.
Tell the Haxe compiler to generate class files for the library.swf by executing this command from the command line:
haxe –gen-hx-classes library.swf
If all goes well you will have a directory structure that looks like this:
hxclasses\com\lia\utils\SWFProfiler.hx
Embeding the SWF file into the Haxe project
Open your Haxe project with FlashDevelop.
Duplicate the directory structure that was generated by the Haxe compiler. The easiest way to do this that I have found is to copy the com directory in Windows explorer and paste it under my src directory in FlashDevelop.
Import the library.swf file into your Haxe project. Again I find it is easy to copy/paste it from Windows explorer into my haxe project.
Rename the library.swf to some thing more meaningful. Eg: SWFProfiler.swf
Right click the SWFProfiler.swf and press the “Add to library” menu option
You should now be able to call the SWFProfiler code from your Haxe code!
For example I have this line of code in my test project and I am able to then use the profiler in my compiled Haxe project.
SWFProfiler.init(flash.Lib.current.stage,flash.Lib.current);
Conclusion
By following these steps you should now be able to compile ActionScript 3 source code into a swc file, extract a libray.swf file from the swc file, generate Haxe classes for each class in the swf, embed the swf and use the embeded objects from a Haxe project in FlashDevelop.
References