Friday, March 23, 2012

Simple Tutorial for using MochiAds with HaXeNME

HaXeNME ( is an open source, cross platform haxe lib for developing games using a Flash like api. Just try it if you don't know it. If you hope to write once and compile to flash/html5/cpp/ios..., HaXeNME should be your first choice. I never tried to use mochiads with haxenme, though I know it is totally possible. Since some one PMed me for this topic, I did a quick research and want to share it here as a reminder for future reference.

Note: this is not a detailed step-by-step tutorial for newbies, just a reminder. I assume you have some experience of developing with HaXeNME using FlashDevelop ( However, if you have problems, feel free to post it here below.

The general steps for using mochiads with HaXeNME:

1. Download mochi api source code and compile all the AS3 source files into a "mochi.swc", unzip the swc file, rename the "library.swf" to "mochi.swf".

2. Generate the haxe source files needed for compiling your NME project:

haxe.exe -swf mochi.swf --no-output -swf-lib mochi.swf --gen-hx-classes 
Copy the genetated "mochi" folder (in its "as3" subfolder you should find lots of haxe files, such as "MochiAd.hx", "MochiCoins.hx", ...) into your project's "src" folder.

3. Import the mochi apis in your haxe project's main class:
import mochi.as3.MochiAd;
import mochi.as3.MochiServices;
To show pregame ads, for example, using the following code, similar to AS3:
MochiServices.connect("60347b2977273733", root);
MochiAd.showPreGameAd( { id:"60347b2977273733", res:"640x580", clip: root} );

4. Edit your project's "application.nmml", add the following line to force the haxe compiler to link "mochi.swf":
<compilerflag name="-swf-lib yourpath/mochi.swf"/>
Make sure the path to "mochi.swf" is correct.

5. Compile your project, that's all.
(Besides, you can use other swf libs with haxenme similarly.)

Full source code of my test project: MochiAds Test
And the demo swf proving it works: MochiAds Test/bin/flash/bin/HaXeNMEMochiAdsTest.swf 

1. NMML File Format:


  1. Thanks for posting this information. It's named as simple, but it's amazing tutorial.

    It really helped me to get my work done which otherwise could have consumed lot of my time to find this technique.

  2. I was getting some odd read-only errors until I passed in the as3_native flag to the haxe compiler. You can do this from the project's NMML file too, like this.


    1. Looks like it stripped out the actual content. :/

      Replace the square brackets with angled ones, and you'll get what you need to put into the NMML file.

      [haxeflag name="as3_native" /]
      [haxeflag name="-swf-lib" value="mochi.swf" if="flash" /]

      Hopefully, it goes through this time...

  3. Great! Thank you very much.

  4. thank you for the valuable info dose this work for haXePunk ?

    1. Never tried but it should work with any NME based projects.

  5. good job man.
    one question, I can compile it. using Haxe for android?

    1. I think that won't work, although I never tried. HaXe for Android will compile everything to native code, but Mochi doesn't provide service for native applications such as Android & IOS apps.So I think this way only works for HaXe targeting Flash.