Creation of a Flash highscores API
- May 12, 2008 by Emanuele Feronato
- Filed under Actionscript 2, Flash, Game design, Tutorials | 24 Comments
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:
1 2 3 4 5 6 | var send_score:LocalConnection = new LocalConnection();
onMouseDown = function () {
points = Math.floor(Math.random()*1000);
_root.score.text = "Your last score: "+points;
send_score.send("hall_of_fame", "compare_scores", points);
}; |
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…
1 2 3 4 5 6 7 8 9 | var get_score:LocalConnection = new LocalConnection();
best_score = 0;
get_score.connect("hall_of_fame");
get_score.compare_scores = function(points):Void {
if (points>best_score) {
_root.score.text = "Highest score: "+points;
best_score = points;
}
}; |
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
They can be easily customized to meet the unique requirements of your project.
24 Responses
Leave a Reply
TUTORIAL SERIES:
- Una guida completa al gioco del poker online e una selezione dei migliori casino online.
- casino online
- migliori casino online
- BlackJack online
- casinò online

(6 votes, average: 4.17 out of 5)

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?
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!
Thanks for this! I tried to make an API like this but failed…now it looks like I won’t!
thanks, this is great!
Got 999!
Great tutorial and very simple to understand.
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?
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 .
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!
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.
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/
[...] that you know how to communicate between two Flash movies thanks to the Creation of a Flash highscores API tutorial, it’s time to introduce the next step that will make you save your highest score on your [...]
Could this method be used to make a serverless multiplayer game?
[...] is a standalone tutorial, but the basics explained will be useful in the Creation of a Flash highscores API [...]
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!
Hey im making a game in Flash CS3 and i need a highscore chart. its a game of snake (yes i know boring) but i need to know how i could make it so when you die it saves the highscore on that page. only running 1 programme. Can someone help me out plz.
Hmm, I dont get it, why do you need to send variables to another swf, and then send your scores to DB again ?
nice.
i would be interested if you have implemented this with any security in mind?
Hi everybody. is Tsearch some sort of hacker software? Who really cares! if somebody wants to use Tsearch then you have a darn popular game!
anyway I suggest you make a clear button which can only be used if you put in a password. so it would be…
on(press){
if(password==”yourpassword”
best_score = 0;
}
Easy!
oops i forgrot to close the brackets for if
Hi Emanuele,
I am a bit confused about the whole local connection thing. If two users are connecting to the same file, would they be able to see the same results? If one user scores 200, would another user be able to withdraw this score from the flash file, or would it only be stored on the user’s computer until they hit refresh?
I understand using flash to save the scores to MySQL, but I am trying to create a game which might be too CPU intensive for the server it is on. Is there a way to send and withdraw data in realtime that would not be a huge load on the server? Like, less than 20% CPU usage?
My game is going to run at 20 fps. I will be using MySQL to store user data, but I want the live actions to be stored in any other way possible that will utilize very little CPU.
Sorry if this is poorly worded.
Cyclone
“Could this method be used to make a serverless multiplayer game?”
That is actually what I was planning on doing.
is there any API which uses actionscript 3?
I second the AS3 idea that would be awesome