Alex Jeffery

HomeArchivesAboutFeed

How To Embed A SWF File In Haxe

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.

  1. <flex-config>
  2.   <compiler>
  3.     <source-path>
  4. <!–
  5. The path-element tells compc where to find the actionscript source files
  6. –>
  7.       <path-element>D:\Flash Games\External\SWF Profiler</path-element>
  8.     </source-path>
  9.   </compiler>
  10. <!–
  11. The output tells compc what to call the compiled file.
  12. –>
  13.   <output>SWFProfiler.swc</output>
  14.   <include-classes>
  15. <!–
  16. The class nodes tell compc wich classes to include.
  17. Add more class nodes to add more classes to the output.
  18. Note: The class files have to be in folders that match their package under the path-element directory.
  19. For example my actionscript file is found here D:\Flash Games\External\SWF Profiler\com\lia\utils\SWFProfiler.as
  20. –>
  21.     <class>com.lia.utils.SWFProfiler</class>
  22.   </include-classes>
  23. </flex-config>
  24.  

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

Comments are closed.

Alex Jeffery