Pages

Sunday, April 7, 2013

Integrating Chartboost with Haxe NME for Android

Haxe NME is a great cross platform framework for game which support wide range of platform from desktop to mobile. It compiles to native, and have the similarity with flash AS3. NME is extensible and have numbers of useful third party library support. I prefer it mostly because it could compile to many different platform especially flash, Android, iOS, and Blackberry10.


maybe the only problem with NME is the lack of documentation and not so active community outside the forum and mailing list. there's still so many things not covered in the tutorial and some googling of NME-Android related things won't give you direct answer to the problem. stackoverflow tag of haxe/nme tag doesn't have so much question and  reddit r/haxe sub just have a small number of reader.

One problem stuck me now is integration of ads, specifically Chartboost . Chartboost is a really good ads network with a high CPM. before using NME I used libgdx to create android games, and chartboost integration with libgdx is easy as breeze because it's covered on the tutorial.

I searched everywhere looking for how to integrate chartboost (Android) on NME and it result on nothing. Luckily, after struggling so much with so many different keyword I stumbled upon gigglingcorpse's blog post and found some answer. I contacted gigglingcorpse and after consulting for a while finally I could integrate the ads! Big Thanks!

I feel the urge to write this tutorial to help others give more understanding about of how to integrate chartboost (Android) with NME. It's not really hard but maybe it could confuse new player which still not understand about how NME work behind the screen.

So let's start!

First thing first! In order to integrate chartboost ads in our game we need to first register the app inside chartboost dashboard. just register on chartboost and Add App to get the APP_KEY and SIGNATURE.   I won't tell you how to do that here. just look it up on the official help yourself. just make sure the test mode enabled . also you need to create campaign and check the created app so the app could display ads inside.

After we got the APP_ID and APP_SIGNATURE we can integrate it in our NME project.

I used FlashDevelop in this tutorial so I will create a HelloWorld project for the purpose of tutorial.

First thing to do is adding chartboost library to the project. in order to do that we need to create libs folder inside assets folder so the structure will be like assets/libs. Download the chartboost android library from the official download and then copy it to libs folder .

add chartboost library

Then on the application.nmml write a template tag to rename it as lib folder in android

<template path="assets/libs" rename="libs" if="android" />

Adding chartboost library is done! Next step is putting the code.

Chartboost integration require us to add some code on Activiy class in Android. So we need to override the default MainActivity.java emplate. MainActivity.java s an android class generated by haxe as the Activity class to launch in android. the default MainActivity.java will looks like a blank class as seen below


package ::APP_PACKAGE::;

import android.os.Bundle;

public class MainActivity extends org.haxe.nme.GameActivity {
}


We cannot add code directly to the generated MainActivity.java class because the code added will be wiped out when we do the next build. In order to override the MainActivity.java class we need to change the MainActivity.java template and fill it with necessary code. To do that , copy MainActivity.java template from nme installation folder. The template located in templates\default\android . copy the MainActivity template to project directory and then register it as template on application.nmml . change the [package_path] with your app package path


<template path="MainActivity.java" rename="src/com/fugo/helloWorld/HelloWorld/MainActivity.java" if="android" />


Open MainActivity.java and add the code as instructed on the official tutorial . there's nothing need to be changed once you got it done so you can just copy paste code below to your MainActivity.java .just remember to change "YOUR_APP_ID" and "YOUR_APP_SIGNATURE" with the APP_ID and APP_SIGNATURE.


package ::APP_PACKAGE::;

import android.os.Bundle;
import com.chartboost.sdk.*;

public class MainActivity extends org.haxe.nme.GameActivity {
 private Chartboost cb;
 
 @Override
  protected void onCreate(Bundle state) {
        super.onCreate(state);   
  
  // Configure Chartboost
  this.cb = Chartboost.sharedChartboost();
  String appId = "YOUR_APP_ID";
  String appSignature = "YOUR_APP_SIGNATURE";
  this.cb.onCreate(this, appId, appSignature, null);
 
  // Notify the beginning of a user session
  this.cb.startSession();

  // Show an interstitial
  this.cb.showInterstitial();

    }
 
 @Override
 protected void onStart() {
  super.onStart();

  this.cb.onStart(this);
 }

 @Override
 protected void onStop() {
  super.onStop();

  this.cb.onStop(this);
 }

 @Override
 protected void onDestroy() {
  super.onDestroy();

  this.cb.onDestroy(this);
 }

 @Override
 public void onBackPressed() {

  // If an interstitial is on screen, close it. Otherwise continue as normal.
  if (this.cb.onBackPressed())
   return;
  else
   super.onBackPressed();
 }
}

we could also use the code above as a template for next project without changing much of the code, just the APP_ID and APP_SIGNATURE.

save up the change and.... test run it on android.

Voila! Chartboost (Android) with HaxeNME
Integration done!
to give more control of the ads showing , you could change the showInterstitial() method with cacheInterstitial() in onCreate() and then call cb.showInterstitial() through JNI call from haxe. for convenience I created a public static method called showChartboost() in MainActivity.java and call it from haxe like below

var showChartboost = JNI.createStaticMethod("com/amagine/plate2/MainActivity", "showChartboost", "()V");
showChartboost();

there.. from here on much can be done via JNI so I'll leave it up to you about the advanced things. thanks for reading!

~End of Tutorial~