2010-02-14 65 views
0

我正在創建一個使用sharedObject來保存每個玩家本地進度的遊戲。它還連接到中央數據庫以創建在線記分牌。當用戶首次插入分數時,唯一ID將從數據庫發送到swf並作爲sharedObject數據的一部分進行保存。Flash AS2 - 幫助將使用PHP從mySQL導入的變量保存到sharedObject中?

絕對一切正常,並且ID保存到sharedObject,但是當swf重新啓動時,ID不會加載(即使保存在sharedObject中的其他變量也會加載)。

我認爲這可能與它被格式化的方式有關,也許與XML有關,但我不確定。

閃存代碼

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代碼

<?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); 
?> 

回答

0

如果我米理解好了,你shuld嘗試通過currID在XML接收對象,以取代playerID因爲atm你把xml回覆放在一個playerID var和你後面的行請撥saveGamecurrID作爲arg,這是未定義的。

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"); 

} 

你應該使用含有savedgame對象正確的內容類型添加到您的PHP頁面

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

最後,我認爲這將是更好地存儲在不同方式的數據(爲例所有與遊戲相關的屬性爲相同的currID)

0

感謝您的答案,但那不是問題。

我認爲問題是,當共享對象存在問題時,將變量存儲爲真實數字,並且只是將所有內容都轉換爲字符串,這對分數很好,但對於Id而言不是必須傳遞給PHP 。

通過使用String('number')將ID轉換爲要存儲在sharedObject中的字符串,然後Number('string')將其從SharedObject拉出時轉換回數字來解決此問題。