Talking about Actionscript 3, Flash, Game development and Monetize.
Last week I showed you how to create your first MochiMedia powered game and enable it for Live Update.
Let’s see what happens now: in your game dashboard you should see something like this:
During this step we’ll see how to connect with Mochi services and enable link tracking.
If you click on “reports” you’ll see you aren’t connecting with MochiServices yet. That is, you are showing an ad in your preloader, but once the game is loaded, you don’t interact with Mochi servers.
Let’s connect to Mochi!
You have to download the official API (you can’t fail, there are links to it everywhere in the dashboard, at the time of writing we are using 3.9.4 which can be found here) and extract the mochi
folder containing the as3
folder in the same folder where your game is placed.
Then, to connect with Mochi services, your Main
class should be:
package {
import flash.display.Sprite;
import mochi.as3.*;
public class Main extends Sprite {
private var _mochiads_game_id:String="7a2e0f0bad9d0c77";
public function Main() {
mochi.as3.MochiServices.connect(_mochiads_game_id, root, noConnection);
var game:gameMc=new gameMc();
addChild(game);
}
private function noConnection(status:String):void {
trace("there is an error: "+status);
}
}
}
Line 3: imports Mochi API
Line 5: this is the line you had to place in your code in order to have Live Updates enabled.
Line 7: connect
method connects to Mochi services. Look at the first argument, which is the game id Mochi gave you when you created the game, and the third argument, the callback function to be executed if something goes wrong.
Lines 11-13: such function just outputs the error type, but can be useful to tell the player some features in the game won’t be accessible.
Test the movie, and in your output window you should see something like this:
MochiServices Connecting...
Waiting for MochiAds services to connect...
[SERVICES_API] connected!
There could be more stuff according to your security sandbox. Check this link for more information about security sandbox, anyway remember you have to check for the “connected” response.
I also want to show you a couple of common error messages which can happen when you enter a wrong id:
Warning: Your game ID (x7a2e0f0bad9d0c77) has been determined to be invalid. This will result in API failures.
or when you cannot reach Mochi servers:
MochiServices Connecting...
Waiting for MochiAds services to connect...
MochiServices could not load.
there is an error: IOError
In this last case also notice how noConnection
function has been called.
Link tracking
Mochi’s link tracking allows you to track the links you placed inside your game, such as the well known “play more games” button or the “credits” button.
Besides the tracking purpose, there are three very interesting features in it:
* You can change the destination of the links directly from the game dashboard
* You can create different link targets according to the domain your game is being played on
* Should Mochi servers go offline, the link will work anyway, although it won’t be tracked and it will link to the default URL.
Look what I did:
I clicked on CREATE NEW LINK
then I created a link called “link to my blog” which links to this blog, but if the SWF is played from emanueleferonato.com domain, then the same link will be redirected to triqui.
This can be useful if you want to redirect players to different sites according to the domain in which they are playing your game
Once you click SAVE
you will be redirected to the link page where you can see all the links you are currently tracking and get the code clicking on the GET CODE
button.
Now, the hardest part.
As you can see in Main function, I call a gameMc
instance which represents the game itself (just the title at the moment).
This is gameMc.as
:
package {
import flash.display.Sprite;
import mochi.as3.*;
import flash.events.Event;
public class gameMc extends Sprite {
public function gameMc() {
/*
WRONG!!
var visitBlog:visitBlogMc=new visitBlogMc();
addChild(visitBlog);
MochiServices.addLinkEvent('http://x.mochiads.com/link/7bb0d7adfc9263d3', 'http://emanueleferonato.com/', visitBlog);
*/
addEventListener(Event.ADDED_TO_STAGE,onAddedToStage);
}
private function onAddedToStage(e:Event):void {
var mochiContainer:Sprite=new Sprite();
stage.addChild(mochiContainer);
var visitBlog:visitBlogMc=new visitBlogMc();
mochiContainer.addChild(visitBlog);
MochiServices.addLinkEvent('http://x.mochiads.com/link/7bb0d7adfc9263d3', 'http://emanueleferonato.com/', mochiContainer);
}
}
}
As you can see, at line 3 the Mochi API is being loaded, but the core of the scripts lies at lines 11-13:
Here I am adding to Display List a visitBlogMc
Display Object which is just a text saying “visit my awesome blog!!”, then adding the Mochi link event to it.
It won’t work, because you have to attach the event to a container Display Object placed on stage.
That’s why I merely wait for gameMc to be added on stage (line 16) then in onAddedToStage
function I am attaching the event.
Line 19: creates a new Sprite called mochiContainer
.
Line 20: adds mochiContainer
to stage.
Line 21: at this time I create a new visitBlogMc
instance.
Line 22: then I adds it as a mochiContainer
‘s child.
Line 23: this is the event listener with its arguments: respectively the link encoded in Mochi format, the link where to go if Mochi servers are offline and the link’s container.
You can even add a fourth argument with a callback function to be executed after the link has been clicked, if you want something to happen when the player clicks on the link.
visitBlogMc
class just defines some properties:
package {
import flash.display.Sprite;
public class visitBlogMc extends Sprite {
public function visitBlogMc() {
buttonMode=true;
x=480;
y=455;
}
}
}
Go this update live!
Once we modified the game adding connection with Mochi services and link tracking, it’s time to go this update live.
In game dashboard, under Game Settings tab click UPLOAD NEW VERSION
and follow the rules already explained in the previous step.
Then, once the game has been uploaded, select the version you want to go live and you’re done.
From now on, all sites running the old version, will show the new one.
Here it is:
Next time, I’ll show you how to add leaderboards. Meanwhile, download the source code.
Never miss an update! Subscribe, and I will bother you by email only when a new game or full source code comes out.