High Score Engine V2.0 For Flash

Introduction

This set of scripts allows you to add high-score functionality to your Flash games, as well as display those high scores in a webpage, styled to your own liking. Feel free to use this, but mention that you got it from flashgamecoders.com.

Requirements

PHP v.4.02
mySQL 3.23.58 or higher

Features

Author

Glen Rhodes - www.glenrhodes.com
Available at www.flashgamecoders.com

Setup

Step 1

The first thing you will have to do is set up the variables in hsconfig.php. Open the file in an editor, and look for these lines of code:

  $hostname_HSDB = "";
  $database_HSDB = ""; 
  $username_HSDB = "";
  $password_HSDB = "";

Fill these in with your mySQL information, for example:

  $hostname_HSDB = "localhost";
  $database_HSDB = "gamedatabase";
  $username_HSDB = "johnny";
  $password_HSDB = "l33tp4zzw0rd";

If you don't know your settings, you must find out from your web host.

Step 2

Copy all included files into a folder called 'hiscore' on your server:

CORE ENGINE

SUPPORT FILES

Step 3

Run the file setup.php from your browser. If your site was called www.dummy.com, and your high score files were in a folder called hiscore, then you'd run:

http://www.dummy.com/hiscore/setup.php

This file will create your database table.

 

Integrating with Flash

RETRIEVE SCORES

To retrieve a score list in Flash, you simply need to call the script, with the viewmode set to "flash" (along with the other required variables).

The returned values will be in a name/value pair set of strings that look like this as raw data:

&n0=Leo&s0=61&n1=Roger&s1=125&n2=Glen&s2=612&n3=Pola&s3=733&n4=Roger&s4=1335&
n5=Shar&s5=1522&n6=Sam&s6=1847&n7=Steve&s7=2373&n8=Pico&s8=2633&n9=Sam&s9=2882
&n10=Mary&s10=6612 

You don't need to worry about this, because Flash will parse these results, and the Flash variables are simply n0, n1, n2, n3, n4 etc., for the player names, and s0, s1, s2, s3, s4 etc., for the corresponding score. The Actionscript code to do this is like so:

 serverURL = "http://www.dummysite.com/hiscore/";
 gameID = "SUPERGAME";
   
 lv = new LoadVars();
 lv.onLoad = function(state)
 {
   
    var i = 0;
    while (this["s" + i] != undefined)
    {
       trace ("Rank " + (i + 1) + " NAME: " + this["n" + i] + 
              " SCORE: " + this["s" + i]); 
       i++;
    }
   
 }
   
 lv.gameID = gameID;
 lv.limit = 10;
 lv.action = "getTop";
 lv.viewmode = "flash";
 lv.dupes = "false";
   
 lv.sendAndLoad(serverURL + "hiscore.php", lv, "POST");

serverURL is the location of your scripts. The above code would load the top 10 scores of game associated with id SUPERGAME. Upon return, the scores would be traced to the screen as:

 Rank 1 NAME: John Doe SCORE: 94391
 Rank 2 NAME: Peter SCORE: 71815
 Rank 3 NAME: Hicks SCORE: 65361
 Rank 4 NAME: Walter SCORE: 59993

See hiscoretest.fla (included in the zipfile) for more examples. It will be up to you to do something more interesting with the scores than just simply tracing them to the trace window. In the included example hiscoretest.fla, there are datagrids on the screen which are populated from the results of the call.

SUBMIT SCORES

To submit a score in Flash, you simply pass in score, userid, gameid and action of "addNew". The following Actionscript does the job:

    serverURL = "http://www.dummysite.com/hiscore/";
gameID = "MYFLASHGAME";

submit_btn.onRelease = function()
{
lv = new LoadVars();
lv.onLoad = function(state)
{
if (this.result == "success")
{
result_txt.text = "Successfully Added Score";
}
else if (this.result = "fail")
{
result_txt.text = this.reason;
}
}

lv.gameID = gameID;
lv.userID = username.text;
lv.score = randomscore.text;
lv.action = "addNew";
lv.sendAndLoad(serverURL + "hiscore.php", lv, "POST");

result_txt.text = "Submitting...";
}

The script will return a variable called "result", which will either be "success" or "fail". If the result has failed, then there will be another variable called "reason" which will contain text explaining the reason for the failure.

All done? Download the zipfile here.

Credit: Glen Rhodes
Flash Version Required: 5, MX, MX 2004
Actionscript version used: 1.0
Difficulty of concept: Intermediate
Subject: Enhancing Flash
Short Biography: Glen Rhodes has been developing games for over 15 years for Sony Playstation, Sega, Windows and Flash, and is the author of 9 books including Macromedia Flash MX 2004 Game Development. Glen has spoken on Game Development at Flashforward and Flashinthecan