Guide to Mochi Media services – connecting with Mochi Services and using Link Tracking

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();
		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 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

package {
	import flash.display.Sprite;
	import mochi.as3.*;
	public class gameMc extends Sprite {
		public function gameMc() {
			var visitBlog:visitBlogMc=new visitBlogMc();
            MochiServices.addLinkEvent('', '', visitBlog);
		private function onAddedToStage(e:Event):void {
			var mochiContainer:Sprite=new Sprite();
			var visitBlog:visitBlogMc=new visitBlogMc();
			MochiServices.addLinkEvent('', '', 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() {

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.

Get the most popular Phaser 3 book

Through 202 pages, 32 source code examples and an Android Studio project you will learn how to build cross platform HTML5 games and create a complete game along the way.

Get the book

// 1+2=3
// 100 rounds
// 10000000
// 2 Cars
// 2048
// A Blocky Christmas
// A Jumping Block
// A Life of Logic
// Angry Birds
// Angry Birds Space
// Artillery
// Astro-PANIC!
// Avoider
// Back to Square One
// Ball Game
// Ball vs Ball
// Ball: Revamped
// Balloon Invasion
// BallPusher
// Ballz
// Bar Balance
// Bejeweled
// Biggification
// Block it
// Blockage
// Bloons
// Boids
// Bombuzal
// Boom Dots
// Bouncing Ball
// Bouncing Ball 2
// Bouncy Light
// BoxHead
// Breakout
// Bricks
// Bubble Chaos
// Bubbles 2
// Card Game
// Castle Ramble
// Chronotron
// Circle Chain
// Circle Path
// Circle Race
// Circular endless runner
// Cirplosion
// CLOCKS - The Game
// Color Hit
// Color Jump
// ColorFill
// Columns
// Concentration
// Crossy Road
// Crush the Castle
// Cube Jump
// CubesOut
// Dash N Blast
// Dashy Panda
// Deflection
// Diamond Digger Saga
// Don't touch the spikes
// Dots
// Down The Mountain
// Drag and Match
// Draw Game
// Drop Wizard
// DROP'd
// Dudeski
// Dungeon Raid
// Educational Game
// Elasticity
// Endless Runner
// Erase Box
// Eskiv
// Farm Heroes Saga
// Filler
// Flappy Bird
// Fling
// Flipping Legend
// Floaty Light
// Fuse Ballz
// GearTaker
// Gem Sweeper
// Globe
// Goat Rider
// Gold Miner
// Grindstone
// GuessNext
// Helicopter
// Hero Emblems
// Hero Slide
// Hexagonal Tiles
// HookPod
// Hop Hop Hop Underwater
// Horizontal Endless Runner
// Hundreds
// Hungry Hero
// Hurry it's Christmas
// InkTd
// Iromeku
// Jet Set Willy
// Jigsaw Game
// Knife Hit
// Knightfall
// Legends of Runeterra
// Lep's World
// Line Rider
// Lumines
// Magick
// MagOrMin
// Mass Attack
// Math Game
// Maze
// Meeblings
// Memdot
// Metro Siberia Underground
// Mike Dangers
// Mikey Hooks
// Nano War
// Nodes
// o:anquan
// One Button Game
// One Tap RPG
// Ononmin
// Pacco
// Perfect Square!
// Perfectionism
// Phyballs
// Pixel Purge
// PixelField
// Planet Revenge
// Plants Vs Zombies
// Platform
// Platform game
// Plus+Plus
// Pocket Snap
// Poker
// Pool
// Pop the Lock
// Pop to Save
// Poux
// Pudi
// Pumpkin Story
// Puppet Bird
// Pyramids of Ra
// qomp
// Quick Switch
// Racing
// Radical
// Rebuild Chile
// Renju
// Rise Above
// Risky Road
// Roguelike
// Roly Poly
// Run Around
// Rush Hour
// SameGame
// SamePhysics
// Security
// Serious Scramblers
// Shrink it
// Sling
// Slingy
// Snowflakes
// Sokoban
// Space Checkers
// Space is Key
// Spellfall
// Spinny Gun
// Splitter
// Spring Ninja
// Sproing
// Stabilize!
// Stack
// Stairs
// Stick Hero
// String Avoider
// Stringy
// Sudoku
// Super Mario Bros
// Surfingers
// Survival Horror
// Talesworth Adventure
// Tetris
// The Impossible Line
// The Moops - Combos of Joy
// The Next Arrow
// Threes
// Tic Tac Toe
// Timberman
// Tiny Wings
// Tipsy Tower
// Toony
// Totem Destroyer
// Tower Defense
// Trick Shot
// Tunnelball
// Turn
// Turnellio
// TwinSpin
// vvvvvv
// Warp Shift
// Way of an Idea
// Whack a Creep
// Wheel of Fortune
// Where's my Water
// Wish Upon a Star
// Word Game
// Wordle
// Worms
// Yanga
// Yeah Bunny
// Zhed
// zNumbers