Implementing MochiAds in a Flash AS3 Class

MochiAds are still in beta, but we here at 8bitrocket are firm supporters of this system. Mochi seems to be one of the few systems that is looking out for the independent developer. You can read more in Steve’s entry from a few weeks back.

I am designing a new game called Pumpkin Man. It is based on Pac man. I could have simply made a Halloween Based Pacman clone hand been done with it in a week or some, but I took the opportunity to spend some time creating a new game loop engine in AS3. The engine that includes a State Machine Gameloop (a little like Moock’s GameConsole if you have his new book) and a generic GamePlay loop for running a game. I am also creating a new rendering engine that uses all bitmaps and no Movieclips. Actually, it uses all bitmapData and one MovieClip to attach all visual changes to much like an Atari ST or Amiga “bit block transfer” or “blitter” as Atari called the chip that finally made it into the STE platform well after the Amiga had taken the ST’s crown as the worlds best computer gaming machine (or at least Europe’s Best). It you are interested in game design at all, you can check out the first entry in the Pacman game redesign called Launching Pad: Redesigning a Retro Classic #1: Pacman. Designing a Maze Chase game is not as easy as it first appears, especially is you don’t want to take shortcuts like many similar Flash Games have. I will be continuing this series this week with more entries on game design, State machines, grid movement, and AI.

In designing the generic GameLoop, which takes care of things like pre-loading, showing Mochiads, title, instruction, high score screens,etc, I found the need to implement the Mochiad platform in AS3. While this is not too difficult, it might be confusing to some, and as I had to hunt an peck around a little to do a full Class based implementation, I figured I would share my experience with others in case anyone else found a need for it.

You will first need to visit and sign up as a beta tester, or if it is in production when you read this, sign up as a game creator. You will be given access to the MochiAd code for as1, as2, and as3. While all three of these can be easily added to the timeline directly, what if, like me, you have a main document class that controls your timeline. In this case, you must implement it inside the document class because mixing a document class (especially a State Machine) and timeline based code will be confusing at best and disastrous at worst. So, without further delay, here are the basic steps to implementing Mochiads in AS3.

Step 1: Make sure the file is in the same root folder as your .fla or in the same class path as your document class. I am going to refer to the document class as from now on.

Step2: Your must be declared Dynamic. This is because the Mochiad Class needs to write to the main Document class and create some variables. It will look like this:
dynamic public class DocumentClass extends MovieClip {

Step3: In your code you will have some place where you want to show an ad. In this example I am using the “showPreloaderAd” because I want to show the ad before the game loads. The call will look something like this:
MochiAd.showPreloaderAd({clip:this, id:”gameifrommochi”, res:”400×400″, ad_finished:this.adFinished});

Let’s break this down into pieces because it is the crux of the implementation.
1. the clip parameter is a reference to the movieClip that will be the parent for the ad. Since we are in the DocumentClass, and it extends MovieClip and is Dynamic, it is the perfect home for the ad, so we pass in “this”.
2.Tthe id parameter is the text string that Mochi gives you when you register a game with them.
3. res is the resolution of the game .fla.
4. The ad_finished let’s you specify a function to call when the ad is done showing. This is the most important part because in a timeline based implementation, Mochi can simply jump to the next frame, but in a DocumentClass based implementation that might be difficult or impossible, so you need to specify this callback function to move on to the next “STATE” on your game.

Here is an example of the function taken directly from my file:

[cc lang=”javascript” width=”550″]
function adFinished():void {
trace(“ad finished”);

Basically once the ad is finished, I have my State Machine jump to the next state. Obviously, your implementation will be different, but as you can see it is pretty simple to accomplish. Your implementation might broadcast an event to tell the next game phase to play (show instructions, etc), or like me, you might set some variables and wait for the State Machine to loop back through and jump to the next state. Just keep in mind that the must be dynamic and the call back must be used to call a function when the ad is finished.


If you enjoyed this post, please consider leaving a comment or subscribing to the RSS feed to have future articles delivered to your feed reader.