Creation of a Flash highscores API

More and more game portals are developing their own Flash API to manage high scores.

Having high scores means giving players the opportunity to compete for a leaderboard, making them play your game more and more. And making you earn more and more.

I am showing you how to make one. I'll use LocalConnection, like, as example, MindJolt.

The LocalConnection class lets you develop SWF files that can send instructions to each other, and if all files are located in the same domain, you won't have security issues.

Nice!

So you need two files: the game itself and the leaderboard file. Once the game is over, I will send the score to the leaderboard file, that will generate the leaderboard itself

The game

In this very hard to develop game, you will have to press a button to generate a random number between 0 and 999

Let's see its actionscript:

ACTIONSCRIPT:
  1. var send_score:LocalConnection = new LocalConnection();
  2. onMouseDown = function () {
  3.     points = Math.floor(Math.random()*1000);
  4.     _root.score.text = "Your last score: "+points;
  5.     send_score.send("hall_of_fame", "compare_scores", points);
  6. };

Line 1: Creation of the LocalConnection variable called send_score

Lines 2-4: Your game script here... score is saved in a variable called points

Line 5: Sending the score. How?

send calls the compare_scores method (the second parameter) on a connection opened with the LocalConnection.connect command we will insert into our leaderboard file. The name of the connection is the first parameter (hall_of_fame) while the score is passed as 3rd parameter, where I pass the points variable

That's all. This means I only have to add 2 lines to my game: line 1 with for the connection, and line 5 to send the score

The leaderboard

This is a very very complex leaderboard that will save only the highest score. Come on, what's the meaning of being 42,455th...

ACTIONSCRIPT:
  1. var get_score:LocalConnection = new LocalConnection();
  2. best_score = 0;
  3. get_score.connect("hall_of_fame");
  4. get_score.compare_scores = function(points):Void  {
  5.     if (points>best_score) {
  6.         _root.score.text = "Highest score: "+points;
  7.         best_score = points;
  8.     }
  9. };

Line 1: Same thing the first line of the game, but the variable here is named get_score

Line 2: Initializing the score to zero.

Line 3: Connecting to the hall_of_fame connection created at line 5 of the game

Line 4: Creation of the compare_scores method passed as the 2nd parameter at line 5 of the game. Look how I pass the score as points variable

Lines 5-8: Creation of the leaderboard

And try the example: this game...

...sends the scores to this leaderboard...

easy and clean.

In a few days, I'll show you how to save scores in a mySql database with a little php

Meanwhile, download the sources and tell me how would you make your mySql interface

If you liked this post buy me a beer (or two)

» 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.

12 Comment(s)

  1. EagleVision | May 12, 2008 | Reply

    Wow. First comment! lol

    I, personally, don’t need this. :)
    Some of my fav. tutorials are prototypes, things you can make games out of, you know?

  2. FrozenHaddock | May 12, 2008 | Reply

    I disagree, I really enjoy tutorials based data transfer, I look forward to SQL databases, because once you know how to pass data between flash and SQL, you can start saving games, with usernames and passwords and the like.

    Nice tut Emanuele, keep it up!

  3. Sunil Patel | May 12, 2008 | Reply

    Thanks for this! I tried to make an API like this but failed…now it looks like I won’t!

  4. bob | May 12, 2008 | Reply

    thanks, this is great!

  5. Andy Cook | May 13, 2008 | Reply

    Got 999!

    Great tutorial and very simple to understand.

  6. Grifo | May 13, 2008 | Reply

    I agree with FrozenHaddock. Even tho prototypes are good for getting an idea done, data transfer info gets you to do things as saving, and that’s a step towards a “real game”, a comercial one.

    Bigger games means more people playing, more people playing means more revenue to developers.

    I might be wrong, but it appears to me that flash games are growing a little bigger. Some NewGrounds games I saw recently seem like an attempt to get the flash to a higher level. That could be very interesting, don’t you think?

  7. Chuck Arellano | May 13, 2008 | Reply

    Thank you for beginning this tutorial series! I see that this approach has a lot of other applications other than high scores - for instance, this can be used to exchange data between the game and chat, ala Kongregate. See http://board.flashkit.com/board/showthread.php?t=756551 .

  8. Javier Lázaro | May 13, 2008 | Reply

    Having highscores increases a game´s replay value.

    Apart from that, I´m interested too in the publishing side of games. I´m building a game site and this article is very interesting to me.

    I´m eager to see the next part, and I´d like to see the continuation of this topic:

    http://www.emanueleferonato.com/2008/04/25/play-flash-games-on-triquicom/

    Good work!

  9. Jon | May 14, 2008 | Reply

    This is a nice concise tutorial that shows how easy it is to put something like this together.

    The main question that I have is about cheaters. Is there a good way to do something to prevent people from reverse engineering the submit that the hi-score SWF file is doing to your server to post high scores?

    This seems like a difficult problem to resolve since your SWF files can be decompiled to reveal your encryption method.

  10. Emanuele Feronato | May 14, 2008 | Reply

    Jon, if you use this method to save highscores, then you have to encrypt your swf.

    Read this post

    http://www.emanueleferonato.com/2008/02/11/protect-your-actionscript-with-amayetas-swf-encrypt/

  11. Matt | May 17, 2008 | Reply

    Could this method be used to make a serverless multiplayer game?

  12. fleshmaker | Jun 3, 2008 | Reply

    Hi Emanuele,
    firstly, i greatly appreciate your blog. Your main intent is very similar to mine :) however, how are you going to solve the cheating in the scoretable entries? I’m not referring to reverse engineering cheaters, i am talking about those cheaters using applications like TSearch (application that change value at the memory address, without decompiling needed). I have been working for nearly six months on solutions to this, i am greatly interested in reading your solution to this problem, if you are going to write about it :)
    Looking forward!

2 Trackback(s)

  1. May 15, 2008: Creation of a Flash highscores API - Step 2 : Emanuele Feronato - italian geek and PROgrammer
  2. Jun 1, 2008: Flash sending, manipulating and receiving data with sendAndLoad : Emanuele Feronato - italian geek and PROgrammer

Post a Comment