Flash Code - Insering a Highscore

Paste the following function inside your Flash document:

function addHighScore(returnFrame:String, name:String, score:Number, id:Number) {
var hstGetUrl = "http://websiteaddress/savescore.php";
var hstID = id;
var lvSend:LoadVars = new LoadVars();

lvSend.onLoad = function(success:Boolean) {
if (success) {
gotoAndPlay(returnFrame);
} else {
trace("failed");
gotoAndPlay(returnFrame);
}
}
lvSend.gameid = hstID;
lvSend.name = name;
lvSend.score = score;
lvSend.sendAndLoad(hstGetUrl, lvSend, "POST");

stop();
}

To use this function we need to pass in the name/scores we want inserted:

addHighScore(returnFrame, name, score, gameid)

You will need to supply the frame to return to once the function is finished, as discussed in the last page. Typically you will create a page in flash will an input text field with a submit button. The following code would be attached to the event handler for the submit button:

 


submitButton.onRelease = function() {
addHighScore("enterscorecontinue", highscoreName.text, score, 1);
}

This would add a score into the database, and resume execution at the frame labelled enterscorecontinue (usually the frame after the one with this code on).

So, that all there is to it. Using the database on your web server also means that it should be a simple process to show your high score tables on a web page.

You can increase the security by means of using a checksum and examining the user agent, this may be the subject of a future tutorial.

 

 

( categories: )