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:

1
2
3
4
5
6
7
8
9
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.

Rate this post: 1 Star2 Stars3 Stars4 Stars5 Stars (2 votes, average: 4.50 out of 5)
Loading ... Loading ...
Flash Templates provided by Template Monster are pre-made web design products developed using Flash technology.
They can be easily customized to meet the unique requirements of your project.
Be my fan on Facebook and follow me on Twitter! Exclusive content for my Facebook fans and Twitter followers

This post has 10 comments

  1. Massimo M.

    on January 6, 2008 at 11:19 pm

    if my score variable is : score
    where i must put “score” ?

  2. Emanuele Feronato

    on January 6, 2008 at 11:33 pm

    replace “your_highscore” with “score”

  3. Frederik J

    on January 7, 2008 at 8:47 am

    Good easy-to-follow tutorial. I’m pretty sure I’ll find this useful!

  4. shiv

    on January 7, 2008 at 11:04 am

    Mochi ads people are innovative.
    Emanuele, why dont you start posting some game design contest of your own by Emanueleferonato.com

    You already know that this site is the most famous game tutorials website (I mean more famous than kirupa or gotoAndPlay())

    You should start a contest. And please make it sensible, not the last one “flash guestbook” one which ended to nowhere.

    I have been following this site since september 06. But still some kind of monotony is present here, which lacks the appeal of such a popular site.

    I hope you understand me.

  5. Emanuele Feronato

    on January 7, 2008 at 8:51 pm

    The guestbook won’t end to nowhere, I am sending a mail to the sponsor right now.

    As for a competition “100% Feronato”, it’s not that easy.

    I am afraid I still don’t have enough visitors to host a competition, but believe me when I say soon this blog won’t be the only resource I provide for Flash coders.

    Then… who knows…

    As for the monotony, I have A LOT of ideas for future tutorials… and I have too much tuts “to be continued” to stop publishing interesting stuff for the next century or two.

    At least, that’s what I hope

  6. Frederik J

    on January 7, 2008 at 9:02 pm

    I agree with shiv. Not only because I want to be a part of that competition but, it will attract a lot more people to the site. Especially if you get a good sponsor, which I think you could, because you are well known.

    Emanuele, everyone says this blog is good, and it IS. I don’t know about your future ideas, but maybe its time to get a “partner” or a “co-writer” to help you. ‘Cause you’ve have a lot to do, and we do all support you, but some of that stuff is hard, or takes time. As I think many body have experienced; it is not always you have the whished time.
    - Just a thought.

    :-) But I can’t complain about anything. You’re doing so great, and it’s really cool that you want to keep this blog online. – I’m so happy I found it!

  7. David

    on January 7, 2008 at 10:22 pm

    does the code need to be placed into the highscores frame?
    is this AS2 or AS3?
    this is a very good tutorial as always, and i am grateful for taking your time.
    i completely agree with both shiv and Frederik J. There should be a contest, to attract more people. This site holds huge potential for those that are learning to create games, and yet some are just wasting it. if they had something to lure them in, then more users would come.

  8. shiv

    on January 8, 2008 at 1:01 pm

    Emanuele,

    I am thankful that you understood my point.
    But the main reason that I wrote to you was that this site is a super-popular site on the net. The “WORLD WIDE WEB”.

    As for the monotony, you can give the site a new avatar. Include polls and tip-of-the-day columns, forums and of course contests.

  9. Christopher

    on January 18, 2008 at 6:24 pm

    Hello, How do i get the variable of your_name for the player, how do i make them choose their name?
    Thanks

  10. Jack

    on August 9, 2010 at 9:57 am

    Hi, thanks for the tutorial! Helped me heaps :)

    Only problem is.. I don’t really understand what you mean by replace your_name with the name of the player. Does that mean when the player submits their name, that is what’ll have to replace ‘your_player’? Kind of confused :S

    Thanks.