How to embed MochiAds highscores in your game

As you should know, MochiAds is not only the largest ad network for Flash games, but it also provides additional – and free – services to its users.

One interesting feature is highscores management.

In the MochiAd.as file you must include in your game in order to show ads, there are two functions, without any documentation but some inline comments, that manage highscores.

Mochi people are developing a new highscores API and it would be ready to be released in January, but I think a little guide about current API will be useful anyway for three reasons:

1) As a web developer, I know that sometimes “January” means “February”

2) At the moment there is not documentation at all about the current API

3) Probably the new API, as all new software, will need some time to be optimized when released

So, let’s see how to use the current API.

Somewhere in your movie, in order to embed MochiAds ads, you have to put this line

MochiAd.showPreGameAd({id:"xxxxxxxxxxxxxxxx", res:"wwwxhhh"});

where xxxxxxxxxxxxxxxx is the 16 figures id Mochiads gives you when you submit a new game and wwwxhhh is the width of your game, the x character and the height of your game (es 550x400). Remember your xxxxxxxxxxxxxxxx id.

Now the entire basic process can be made in a few lines. When the game is over and it’s time to submit the score, write:

var highscoreobj:Object = new Object();
highscoreobj.highscoremet = function(scores, position) {
	for (x=0; x<50; x++) {
		name = scores[x][0];
		score = scores[x][1];
		timestamp = scores[x][2];
	}
};
MochiAd.sendHighScore({id:"xxxxxxxxxxxxxxxx", name:your_name, score:your_highscore},highscoreobj,"highscoremet");

Line 1: Defining a new object called highscoreobj

Line 2: Defining a new method for the highscoreobj called highscoremet. The method has two parameters. The first is the array storing the highscores, the second is the position of the last submitted score, or -1 if it's not in the top 50. MochiAds will store "only" the top 50 scores.

Line 3: Beginning a loop from 0 to 49. The API will save the top 50 scores, but it's not mandatory to display them all. If you want to display only the top 10, change the 50 with a 10. Just remember highscores are stored in an array, so the first element is at idex 0.

Lines 4-6: Retrieving the xth ranked name, score and timestamp.

We can represent the scores array in this way

[[name of the 1st, score of the 1st, millisecond timestamp of the 1st], [name of the 2nd, score of the 2nd, millisecond timestamp of the 2nd], ...]

after line 6 and before line 7 you will have to insert your code to display the highscores with an optional message like "congratulations you ranked position-th in top 50" or "oh no! you are not in top 50", according to position value.

Line 9: This is how we submit the score achieved by the player. The only things you have to change, if you simply cut/paste my code, is your_name with the name of the player and your_highscore with the score of the player.
Change the xxxxxxxxxxxxxxxx with the id provided by MochiAds and you are ready to host an highscore table. The remaining two parameters refer to the object and method to call when the score is submitted.

That's all... a complete and global higscores table in just a few rows... thanks to MochiAds.

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

214 GAME PROTOTYPES EXPLAINED WITH SOURCE CODE
// 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
// Save the Totem
// Security
// Serious Scramblers
// Shrink it
// Sling
// Slingy
// Snowflakes
// Sokoban
// Space Checkers
// Space is Key
// Spellfall
// Spinny Gun
// Splitter
// Spring Ninja
// Sproing
// Stabilize!
// Stack
// 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