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:

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

Get the most popular Phaser 3 book

Through 202 pages, 32 source code examples and an Android Studio project you will learn how to build cross platform HTML5 games and create a complete game along the way.

Get the book

215 GAME PROTOTYPES EXPLAINED WITH SOURCE CODE
// 1+2=3
// 100 rounds
// 10000000
// 2 Cars
// 2048
// A Blocky Christmas
// A Jumping Block
// A Life of Logic
// Angry Birds
// Angry Birds Space
// Artillery
// Astro-PANIC!
// Avoider
// Back to Square One
// Ball Game
// Ball vs Ball
// Ball: Revamped
// Balloon Invasion
// BallPusher
// Ballz
// Bar Balance
// Bejeweled
// Biggification
// Block it
// Blockage
// Bloons
// Boids
// Bombuzal
// Boom Dots
// Bouncing Ball
// Bouncing Ball 2
// Bouncy Light
// BoxHead
// Breakout
// Bricks
// Bubble Chaos
// Bubbles 2
// Card Game
// Castle Ramble
// Chronotron
// Circle Chain
// Circle Path
// Circle Race
// Circular endless runner
// Cirplosion
// CLOCKS - The Game
// Color Hit
// Color Jump
// ColorFill
// Columns
// Concentration
// Crossy Road
// Crush the Castle
// Cube Jump
// CubesOut
// Dash N Blast
// Dashy Panda
// Deflection
// Diamond Digger Saga
// Don't touch the spikes
// Dots
// Down The Mountain
// Drag and Match
// Draw Game
// Drop Wizard
// DROP'd
// Dudeski
// Dungeon Raid
// Educational Game
// Elasticity
// Endless Runner
// Erase Box
// Eskiv
// Farm Heroes Saga
// Filler
// Flappy Bird
// Fling
// Flipping Legend
// Floaty Light
// Fuse Ballz
// GearTaker
// Gem Sweeper
// Globe
// Goat Rider
// Gold Miner
// Grindstone
// GuessNext
// Helicopter
// Hero Emblems
// Hero Slide
// Hexagonal Tiles
// HookPod
// Hop Hop Hop Underwater
// Horizontal Endless Runner
// Hundreds
// Hungry Hero
// Hurry it's Christmas
// InkTd
// Iromeku
// Jet Set Willy
// Jigsaw Game
// Knife Hit
// Knightfall
// Legends of Runeterra
// Lep's World
// Line Rider
// Lumines
// Magick
// MagOrMin
// Mass Attack
// Math Game
// Maze
// Meeblings
// Memdot
// Metro Siberia Underground
// Mike Dangers
// Mikey Hooks
// Nano War
// Nodes
// o:anquan
// One Button Game
// One Tap RPG
// Ononmin
// Pacco
// Perfect Square!
// Perfectionism
// Phyballs
// Pixel Purge
// PixelField
// Planet Revenge
// Plants Vs Zombies
// Platform
// Platform game
// Plus+Plus
// Pocket Snap
// Poker
// Pool
// Pop the Lock
// Pop to Save
// Poux
// Pudi
// Pumpkin Story
// Puppet Bird
// Pyramids of Ra
// qomp
// Quick Switch
// Racing
// Radical
// Rebuild Chile
// Renju
// Rise Above
// Risky Road
// Roguelike
// Roly Poly
// Run Around
// Rush Hour
// SameGame
// SamePhysics
// Save the Totem
// Security
// Serious Scramblers
// Shrink it
// Sling
// Slingy
// Snowflakes
// Sokoban
// Space Checkers
// Space is Key
// Spellfall
// Spinny Gun
// Splitter
// Spring Ninja
// Sproing
// Stabilize!
// Stack
// Stairs
// Stick Hero
// String Avoider
// Stringy
// Sudoku
// Super Mario Bros
// Surfingers
// Survival Horror
// Talesworth Adventure
// Tetris
// The Impossible Line
// The Moops - Combos of Joy
// The Next Arrow
// Threes
// Tic Tac Toe
// Timberman
// Tiny Wings
// Tipsy Tower
// Toony
// Totem Destroyer
// Tower Defense
// Trick Shot
// Tunnelball
// Turn
// Turnellio
// TwinSpin
// vvvvvv
// Warp Shift
// Way of an Idea
// Whack a Creep
// Wheel of Fortune
// Where's my Water
// Wish Upon a Star
// Word Game
// Wordle
// Worms
// Yanga
// Yeah Bunny
// Zhed
// zNumbers