kcnhgames.com header

How to use Mochi API with Flixel and actionscript 3 Flash

Edit 2017-04-28: Please note that Mochimedia has been offline for a couple of years now, so the example below cannot be used anymore. I’ve left this blog post intact for historic purposes.

As a supplement to my Flixel Adventure Game Tutorial, I have written this post to explain how to add Mochi ads to your Flixel game.

First, go to your Mochi Media dashboard, if you do not have an account you can sign up for free here. Go to My Dashboard -> Developer Tool -> Add Game and fill in the information required, tick the ‘yes’ button for live-updates, then click ‘Create Game’.

Mochi Ads in Flixel

On the next page, download the file containing the Mochi API and unzip it, copy the ‘mochi’ folder to your project’s ‘src’ folder.

Copy the Mochi folder as seen here.

Create a new file named ‘Preloader.as’ in the same folder as your ‘Main.as’.

// Mochi Ads Preloader.as
package 
{
  import flash.display.DisplayObject;
  import flash.display.InteractiveObject;
  import flash.display.MovieClip;
  import flash.events.IOErrorEvent;
  import flash.utils.getDefinitionByName;
  import mochi.as3.*;
  // Must be dynamic!
  public dynamic class Preloader extends MovieClip 
  {
    // Keep track to see if an ad loaded or not
    private var did_load:Boolean;

    // Change this class name to your main class
    public static var MAIN_CLASS:String = "Main";

    // Substitute these for what's in the MochiAd code
    public static var GAME_OPTIONS:Object = { id: "mochi_game_id", res:"640x480" };
		
    public function Preloader() 
    {
      super();

      var f:Function = function(ev:IOErrorEvent):void 
      {
        // Ignore event to prevent unhandled error exception
      }
      loaderInfo.addEventListener(IOErrorEvent.IO_ERROR, f);

      var opts:Object = {};
      for (var k:String in GAME_OPTIONS) 
      {
        opts[k] = GAME_OPTIONS[k];
      }

      opts.ad_started = function ():void 
      {
        did_load = true;
      }

      opts.ad_finished = function ():void 
      {
        // don't directly reference the class, otherwise it will be
        // loaded before the preloader can begin
        var mainClass:Class = Class(getDefinitionByName(MAIN_CLASS));
        var app:Object = new mainClass();
        addChild(app as DisplayObject);
				
        // Set focus on stage after loading ad.
        stage.focus = app as InteractiveObject;
      }

      opts.clip = this;
      //opts.skip = true;

      MochiAd.showPreGameAd(opts);
      }
   }
}

Copy the mochi_game_id to your clipboard from the second Mochi page (select the text and press control+c). For security reasons I have replaced mine with the text ‘game_id’.

Game id is secret.

Paste your mochi_game_id in the ‘id’ part of this variable in Preloader.as:
Don’t forget to also adjust the resolution of your game if yours isn’t equal to 640×480!

public static var GAME_OPTIONS:Object = { id: "mochi_game_id", res:"640x480" };

Open Main.as and add the following line after the import statements in the file:

[Frame(factoryClass = "Preloader")]

Re-compile your game and upload it to Mochi, if everything went well you will be able to run your game and see Mochi-ads loading!

Now all you have to do is fill in the rest of the details over at the overview page of your game at the Mochi dashboard. Please note that it might take Mochi a day before advertisements will be approved for your game.

This entry was posted in Game development, Tutorials and tagged , , , , , , , , , . Bookmark the permalink. Follow any comments here with the RSS feed for this post. Post a comment or leave a trackback: Trackback URL.

One Comment


  1. Posted January 9, 2014 at 20:57 | Permalink

    Thank you, i was waiting for this post 🙂

    I hope to release soon my first game ( a worthless clone of the pong ) but surely will be a total failure. And as i already told you through Newgrounds I’ll be waiting for your next game. Keep it up!


Post a Comment

Your email is never published nor shared. Required fields are marked *

*
*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>