Demonstrates how to create a .swc library for ActionScript 3.0 using OpenFL and Haxe. This allows AS3 developers using Adobe AIR to access Haxe libraries too.
To build the .swc library, use the following command:
openfl build flash -debug
The file will be created at bin/flash/bin/mylibrary.swc.
To use the .swc library in an ActionScript project, add it with the --library-path
compiler option.
mxmlc --library-path+=mylibrary.swc
During startup, such as in the constructor of the main AS3 class, you should add the following AS3 code because it initializes a few things that are expected by Haxe-compiled .swc libraries:
haxe.initSwc(null);
The OpenFL library contains a number of template files that are used when building a project. You can find them in the openfl/assets/templates and the lime/templates directories. Using the <template>
element in project.xml, it's possible to replace one or more of these template files with custom versions, on a per-project basis, and without forking OpenFL.
This sample project contains a directory named custom-templates. It is configured in project.xml like this:
<template path="custom-templates"/>
Inside custom-templates, there's a directory flash/hxml that contains custom .hxml files for compiling with Haxe, including debug.hxml, release.hxml, and final.hxml. The relative path to this directory inside custom-templates matches the directory named flash/hxml inside Lime's templates directory.
These custom .hxml template files modify the -swf
compiler option to use the file extension .swc instead of .swf for the name of the output file. This is how you configure the Haxe compiler to generate a .swc library for Adobe AIR.
This sample uses the Compiler.include()
macro to include all classes (recrusively) in the com.example package. This macro is configured in project.xml like this:
<haxeflag name="--macro" value="include('com.example')"/>
Finally, a custom AddFlashPropertyMeta.find()
macro is used to automatically add @:flash.property
meta to all properties with getters and setters. Normally, you'd need to do this manually. Haxe's @:flash.property
meta ensures that a property is compiled with native AS3 getters and setters, instead of get_propertyName()
and set_propertyName()
methods. This macro is configured in project.xml like this:
<source path="build_macros/flash"/>
<haxeflag name="--macro" value="AddFlashPropertyMeta.find('com.example')"/>
Sample created by Josh Tynjala, the author of Feathers UI and core contributor to OpenFL.