AS3: Site selective API integration

I know, again a post about API integration. But this time I am pretty sure it runs, because it’s tested twice. And Kongregate as well as Newgrounds ‘answered’ the API call though the game was wrapped by Mochi’s Live Update.

That is the main function in DocumentClass.as which serves as the DocumentClass:

public function DocumentClass ()
{
 MochiBot.track(this, "xxxxxxxx");
 var _mochiads_game_id:String = "xxxxxxxxxxxxxxxx";
 MochiServices.connect("xxxxxxxxxxxxxxxx", root);
 this.addEventListener(Event.ADDED_TO_STAGE, initGame, false, 0, true);
}

public function initGame(event:Event)
{
 domainCheck = LoaderInfo(root.loaderInfo.loader.loaderInfo).url;
             //use for testing: domainCheck = LoaderInfo(root.loaderInfo).url;
 if ( domainCheck.indexOf("kongregate") >= 0 )
 {
             //to check at game end
  kongEnabled = true;
  var paramObj:Object = LoaderInfo(root.loaderInfo.loader.loaderInfo).parameters;
  var api_url:String = paramObj.api_path || "http://www.kongregate.com/flash/API_AS3_Local.swf";
  var request:URLRequest = new URLRequest ( api_url );
  var loader:Loader = new Loader();
  loader.contentLoaderInfo.addEventListener ( Event.COMPLETE, loadComplete );
  loader.load ( request );
  this.addChild ( loader );
  function loadComplete ( event:Event ):void
  {
   kongregate = event.target.content;
   kongregate.services.connect();
  }
 }
 if (domainCheck.indexOf("newgrounds") >= 0 || domainCheck.indexOf("ungrounded") >= 0 )
 {
             //to check at game end
  newgEnabled = true;
  NewgroundsAPI.linkAPI(this);
  NewgroundsAPI.connectMovie(xxxx);
  NewgroundsAPI.addCustomEvent(1, 'Easy Mode Completed');
  NewgroundsAPI.addCustomEvent(2, 'Normal Mode Completed');
  NewgroundsAPI.addCustomEvent(3, 'Hard Mode Completed');
 }
 playLogo();
}

Don’t forget to make kongregate a public variable (public var kongregate:*). And copy the Newgrounds API class into your working folder.

From your game’s end you could call this function (within the Document Class):

public function showHighScore()
{
 if (kongEnabled == true)
 {
  kongregate.stats.submit("StatName", Number);
  if(gameMode == "whatever" )
  {
             //whatever achievement you like
  }
 }
 if (newgEnabled == true)
 {
             //whatever achievement you like
             //NG gives you the code for custom events
 }
}

So, ridin’ t’ tide. Yoho!

EDIT: And it seems to work with MindJolt too. Integrated and scores sent, but the game isn’t approved yet. So, let’s wait…

This entry was posted in mochiads, monetization and tagged , , , , , , , , . Bookmark the permalink.
Be Sociable, Share!

Leave a Reply

Your email address will not be published.