Do you like my tutorials?

Then consider supporting me on Ko-fi

Talking about Flash and Game development.

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.

Never miss an update! Subscribe, and I will bother you by email only when a new game or full source code comes out.