Do you like my tutorials?

Then consider supporting me on Ko-fi

Talking about Actionscript 2, Flash, Game development and Tutorials.

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:

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…

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

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