Saturday, November 15, 2014

Create a SWC for Flash & AS3 Projects from OpenFL

If your code is pure haxe, it's easy to generate an AS3 library swc file for your AS3 projects, as documented at http://old.haxe.org/manual/swc:

haxe -swf mylib.swc MyLibClasses --macro include('mypackage')
For example, to generate the "TextParser.swc" from "TextParser.hx", I used
haxe -swf TextParser.swc TextParser.hx
However, if your project is based on the OpenFL library, more efforts may be needed. Anyway, since you can compile your project to a swf, one ultimated way is to load the external swf at run time and get all public classes and methods inside using the "getDefinition" function from the "ApplicationDomain" class, see for example http://flassari.is/2008/07/swf-class-explorer-for-as3/. However, this way is not the most efficient and need you to write more code. For the most cases, it is possible to generate a swc file from your OpenFL based code. Things you need to do are
1. Rename the package name "openfl" to "flash", e.g., "openfl.display.Sprite" to "flash.display.Sprite".
2. Make your code independent of the openfl only classes and methods. For example, don't use "openfl.Assets.getBitmapData" directly in your code, use a external wrapper object as the assets host such as "myAssetsHost" and redirect all function calls to its public method which wraps "openfl.Assets.getBitmapData". Then in your AS3 project, you can reimplement the wrapper object using AS3 methods for handling assets.
An example is the JiuGongGe.swc, I refactored the original source code to make it independent of "openfl.Assets". It also depends on the library "Actuate", so I copied the library folder "motion" from the installed path "D:\HaxeToolkit\haxe\lib\actuate\1,7,5\motion" to the folder contains the source code of JiuGongGe, then use the command
haxe -swf JiuGongGe.swc JGG.hx  --no-traces --macro include('motion.Actuate')
to get the swc file.

Links:
http://old.haxe.org/manual/swc#creating-swc-with-haxe
http://www.openfl.org/archive/community/general-discussion/how-create-swc-openfl/
https://groups.google.com/forum/#!topic/haxelang/sld9ov4D-tA
http://stackoverflow.com/questions/13020201/how-could-i-convert-an-existing-swf-file-to-an-swc-for-using-as-a-library

No comments:

Post a Comment

Sponsors