PDA

View Full Version : LoadVars high score table


simon373
07-17-2007, 06:19 PM
Hi,

I am trying to set up a high score table using php/mysql. I have the following actionscript inserted in the last frame of my game. It is designed to retrieve the high scores from a database and display them only.

var select_lv:LoadVars = new LoadVars();
// create a string to show where to access the PHP script
var filepath:String;

// set the path to the PHP script based on whether this is being tested
// locally or online. If local, use script saved on online webserver.
// Can use http://localhost instead if script is on local webserver.

if (_url.indexOf("http") != 0) filepath = "http://";
else filepath = "/";

select_lv.send(filepath + "getscores.php", "newwin", "GET");


select_lv.onLoad = function(ok:Boolean) {
if (ok) {
if (this.errorcode=="0") {
for (var i:Number=0; i < this.n; i++) {
scoreInfo.push(
{record:this["id"+i],
nickname:this["nickname"+i],
score:Number(this["score"+i]),
dateposted:this["dateposted"+i]
});
}
// only display Nickname, Score, and Date Posted (not record id)
scores_dg.columnNames = ["nickname", "score", "dateposted"];
// set formatting of nickname column
scores_dg.getColumnAt(0).width = 200;
// trap header click event to sort case-insensitive on this field
scores_dg.getColumnAt(0).sortOnHeaderRelease = false;
// this property will keep track of whether sort is ascending or descending
scores_dg.getColumnAt(0).sortedUp = false;
scores_dg.getColumnAt(0).headerText = "Nickname";

// set formatting of score column
scores_dg.getColumnAt(1).width = 100;
// trap header click event to sort numerically
scores_dg.getColumnAt(1).sortOnHeaderRelease = false;
scores_dg.getColumnAt(1).sortedUp = false;
scores_dg.getColumnAt(1).headerText = "Score";

// set formatting of date column
// auto-sort will work fine for this column
scores_dg.getColumnAt(2).width = 160;
scores_dg.getColumnAt(2).headerText = "Date Posted";

// set dataProvider for datagrid
scores_dg.dataProvider = scoreInfo;

// execute headerRelease function for correct sort when user clicks a header
scores_dg.addEventListener("headerRelease", headerListener);

msg_ta.text = "Enter data and click Add to add a score. Click a row and Delete ";
msg_ta.text += "Selected to delete a score. First four entries may not be deleted.";
} else {
// show kind of error
msg_ta.text = errorMsgs[Number(this.errorcode)];
// if query error, show mysql_error
if (this.errorcode == "3") msg_ta.text += ": " + this.msg;
}
} else {
// if loadvars failed (eg, if script not found)
msg_ta.text = "Flash-database select operation failed";
}
}
msg_ta.text = "Getting high scores from database...";
select_lv.sendAndLoad(filepath + "getscores.php", select_lv, "GET");

stop();

I have inserted the line below to check that data is being retrieved from the database.

select_lv.send(filepath + "getscores.php", "newwin", "GET");

It produces the following in a new browser window:

&errorcode=0&n=4&id0=1&nickname0=Spiderman&dateposted0=2005-09-07&score0=580&id1=2&nickname1=Black Widow&dateposted1=2005-02-01&score1=730&id2=3&nickname2=Hulk&dateposted2=2005-09-10&score2=150&id3=4&nickname3=Underdog&dateposted3=2005-03-05&score3=330&

My DataGrid within the flash movie displays the Nickname, score and date posted headers but does not display any of the high score data. It is blank and there is no error message.

Any ideas what the problem might be?

Many thanks.

Simon

simon373
07-17-2007, 10:11 PM
The below was missing from the code. Needed to create an array to store the results of the for loop.

// create an array to be the dataprovider for the datagrid
var scoreInfo:Array = [];