Flash Code - Fetching Highscores

Now we have our two php scripts to manipulate the database, all we need to do is call these pages from our Flash code (or whatever language your game maybe coded in). Include the following code near the start of your Flash document:

var aHighName = new Array();
var aHighScore = new Array();

function getHighScores(returnFrame:String, id:Number) {
var hstGetUrl = "http://websiteaddress/getscores.php";
var hstID = id;

var lvGet:LoadVars = new LoadVars();

lvGet.onLoad = function(success:Boolean) {
if (success) {
// parse the XML
parseXML(lvGet.xml);
gotoAndPlay(returnFrame);
} else {
trace("failed to fetch hiscores");
gotoAndPlay(returnFrame);
}
}
lvGet.gameid = hstID;
LVgET.uid = getTimer();
lvGet.sendAndLoad(hstGetUrl, lvGet, "POST");

stop();
}

function parseXML(text:String) {
var xmlData:XML = new XML(text);

var aXMLNames = mx.xpath.XPathAPI.selectNodeList(xmlData.firstChild, "/scores/name");
var aXMLScores = mx.xpath.XPathAPI.selectNodeList(xmlData.firstChild, "/scores/score");

for ( var i:Number = 0; i < 10; i++ ) {
aHighName[i] = "";
aHighScore[i] = 0;
}

for ( var i:Number = 0; i < aXMLNames.length; i++ ) {
aHighName[i] = aXMLNames[i].firstChild.nodeValue;
aHighScore[i] = aXMLScores[i].firstChild.nodeValue;
}
}

The magic is all done here with the LoadVars class. This is how flash fetches remote web pages, it can send variables to a page, and fetch them back. The great thing about LoadVars is that it opens up all kind of possiblilites for managing server-side processes. For example, I have a php script which sends an email to a user, this is used by Flash for a 'tell a friend' button in a game.

Let's look at LoadVars for a second. To pass parameters to a php (or asp) script, we need to use the following syntax:

var lvGet:LoadVars = new LoadVars();

lvGet.var1 = "Bob"
lvGet.var2 = "Bill"
lvGet.sendAndLoad(url, lvGet, "POST");

Inside the server side php or asp script, you will be able to read back the variables var1 and var2 eg.:

$_REQUEST["var1"] (will be "Bob")

$_REQUEST["var2"] (will be "Bill")

To get values back from the php script, you create a list of name/value pairs. Eg:

xml=<scores><name>Kevin</name><score>100</score></scores>&numscores=10

This can be read back into as:

lvGet.xml

lvGet.numscores

inside your ActionScript.

There is one final complication - isn't there always! The saveAndLoad() method is asynchronous; this means the web page is loaded in the background. Normally, this is a good idea, however in this case it presents a problem because we don't really want to continue our program until the web page is downloaded. This is why the function stops until the web page is downloaded (using the event handler) and the playhead is resumed at the frame passed into the function as a parameter. To use the function, we need to put the following code inside a frame:

getHighScores("highscoretable", 1);

The first parameter is a frame label where the playhead should resume once the high scores are loaded. The second parameter is the gameid as discussed above.

Once the function is executed, aHighName() and aHighScore() will be an array of the top 10 high scores.

 

( categories: )