Friday, March 23, 2012

Simple Tutorial for using MochiAds with HaXeNME

HaXeNME (http://www.haxenme.org) 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 (http://www.flashdevelop.org). 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:
https://flaswf.googlecode.com/svn/trunk/flaswfblog/Tutorials/HaXeNME MochiAds Test
And the demo swf proving it works:
https://flaswf.googlecode.com/svn/trunk/flaswfblog/Tutorials/HaXeNME MochiAds Test/bin/flash/bin/HaXeNMEMochiAdsTest.swf 

Links: 
1. NMML File Format: http://www.haxenme.org/developers/documentation/nmml-project-files/
2. http://haxe.org/doc/start/flash#using-the-library
http://www.alexjeffery.org/programming/how-to-embed-a-swf-file-in-haxe/
http://wootfu.com/2011/08/using-mochiads-with-haxe/
http://brianin3d.wordpress.com/2010/08/04/kit-kat-toe-mochiads-with-haxe/

8 comments:

  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.

    ReplyDelete
  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.




    Source: https://code.google.com/p/haxe/issues/detail?id=992

    ReplyDelete
    Replies
    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...

      Delete
  3. Great! Thank you very much.

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

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

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

    ReplyDelete
    Replies
    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.

      Delete

Sponsors