Aug 29 2011


If you remember my last HaXe tutorial, on getting a preloader working? Here’s another, on getting preloader ads working. Specifically, on compiling and linking a HaXe project with the Mochi API, which isn’t available for HaXe. This could be extended to getting the Playtomic API working with HaXe, or damn near anything else that just has an AS3 API.

Using the converted API:

I’ve helpfully uploaded a copy of version 3.9.4 of the Mochi API ported for HaXe.


Let’s assume you’re Sam HaXe to link, as I covered before. Copy the “mochi” folder from that ZIP into your source directory. Copy the .SWF somewhere convenient. Then, open your Sam HaXe resource description file – the XML file that lists all the resources – and in frame 1, add:

<swf:library import="mochi.swf" symbols="true" />

…providing the correct relative path if necessary. Now you’re done!

But… let’s say you want to port the API yourself – say, if there’s a newer version out. This is a slightly convoluted process, but just follow the steps…

1. Download the AS3 API and compile it to an SWF.

Download the API and extract it somewhere. Then, find compc.exe. It’s part of the Flex SDK. If you have FlashDevelop installed, you’ll most likely find it in Program Files/FlashDevelop/Tools/flexsdk/bin/.

I’m on Windows, so I used Cygwin – which gives you a Linux-type shell – to run compc as shown in that mailing list example. Install it, start a Cygwin Bash Shell, and navigate to the place you extracted the Mochi API. In my case, that was /cygdrive/c/Coding/Tools/MochiAPI_v3_9_4/. (Yes, in Cygwin, your hard drives are hidden in /cygdrive/). Then run this command:

"/cygdrive/c/Program Files (x86)/FlashDevelop/Tools/flexsdk/bin/compc.exe" -source-path . -output mochi.swc -include-classes `find mochi/as3/ -regex .*as | awk '{gsub(/\.\//, "", $0); gsub(/\.as/, "", $0); print $0}'`

…adjusting the path if necessary.

All that script fu at the end is doing is passing all the .as files in mochi/as3/ to compc.exe without their extensions, as compc expects to put the extensions back on itself. You could do this in a Windows command prompt by replacing that section with a list of the .as files with full relative addresses and without their extensions.

This will generate “mochi.swc”. A SWC is simply a ZIP file with an SWF in it, so rename it to “.ZIP” and extract the delicious SWF inside!

2. Build the API headers from the SWF.

Locate haxe.exe. On my machine, this is in C:\Program Files (x86)\Motion-Twin\haxe\.

You can do this in a command prompt or Cygwin, it doesn’t matter. Navigate to the SWF you extracted and run the following:

"/cygdrive/c/Program Files (x86)/Motion-Twin/haxe/haxe.exe" -swf mochi.swf --no-output -swf-lib mochi.swf --gen-hx-classes

After that completes, you’ll find the generated HaXe API in /haxe/hxclasses/mochi/. Ignore everything else in /haxe/hxclasses/ and copy the mochi/ directory (not the contents of the directory, the directory itself) to your project’s source directory.

That’s it! 🙂