简体   繁体   中英

Flash AS2 - Help saving variable imported from mySQL using PHP into sharedObject?

I am creating a game which uses the sharedObject to save each players progress locally. It also connects to a central database to create an online scoreboard. When a user inserts a score for the first time a unique ID is sent out of the database to the swf and saved as part of the sharedObject data.

Absolutely everything works and the ID is saved to the sharedObject, however when the swf is restarted the ID does not load (even though the other variables saved in the sharedObject do load).

I think it may be to do with the way it is formatted, perhaps to do with the XML but I'm not sure.

FLASH CODE

function saveGame(currID:Number) {
 gameInfo.data["playername"+currID] = playername;
 gameInfo.data["playerscore"+currID] = playerscore;
 gameInfo.data["playerID"+currID] = playerID;
 gameInfo.data["playerLevel"+currID] = playerLevel;
 for(i=1; i<6; i++){
 gameInfo.data["level"+i+"Score"+currID] = ["level"+i+"Score"];
 }
 gameInfo.flush();
}

function loadGame(currID:Number) {
 playername = gameInfo.data["playername"+currID];
 playerscore = gameInfo.data["playerscore"+currID];
 playerID = gameInfo.data["playerID"+currID];
 playerLevel = gameInfo.data["playerLevel"+currID];
}

function scoreboardSubmit() {

var insertReceive:XML = new XML();
insertReceive.ignoreWhite = true;
insertReceive.onLoad = function() {
playerID = this.firstChild.childNodes[0];
saveGame(currID);
};
insertSend = new LoadVars();
insertSend.playername = playername;
insertSend.playerscore = playerscore;
insertSend.playerID = playerID;
insertSend.sendAndLoad("scoreboardSend.php", insertReceive, "POST");

}

PHP CODE

<?php


$name = strip_tags($_POST['playername']);
$score = $_POST['playerscore'];
$id = $_POST['playerID'];

$con = mysql_connect("localhost","******","******");

mysql_select_db("******", $con);


if ($id == 0)
{

$insert="INSERT INTO scoreboard (Name, Score)
VALUES
('$name','$score')";

mysql_query($insert,$con);


$returnID = mysql_query("SELECT LAST_INSERT_ID()");

$playerID = mysql_result($returnID,0);

echo "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n";
echo "<returnID>" . $playerID . "</returnID>\n";

}
else
{

$update = mysql_query("UPDATE scoreboard SET Name = '$name', Score = '$score'
WHERE id = '$id'",$con);

}


mysql_close($con);
?> 

If I m understanding well, you shuld try to replace playerID by currID in your XML receiver object because atm you put the xml reply in a playerID var and the line after you call saveGame with currID as arg which is undefined.

function scoreboardSubmit() {

 var insertReceive:XML = new XML();
 insertReceive.ignoreWhite = true;
 insertReceive.onLoad = function() {
  var currID = this.firstChild.childNodes[0];
  saveGame(currID);
 };
insertSend = new LoadVars();
insertSend.playername = playername;
insertSend.playerscore = playerscore;
insertSend.playerID = playerID;
insertSend.sendAndLoad("scoreboardSend.php", insertReceive, "POST");

}

You should add the correct content type to your php page

header('Content-type: text/xml');

Finally I think it would be better to store your data in a different way (for exemple by using a savedgame object which contains all game related properties for a same currID)

Thanks for the answer but that wasn't the problem.

I think the problem was that when the sharedObject was having problems storing variable as true numbers and was just converting everything to strings, which was fine for the score but not for the Id as that had to be passed on to the PHP.

Solved it by using String('number') to convert the ID to a string to be stored in the sharedObject and then Number('string') to convert it back to a number when pulling it from the sharedObject.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM