好的...所以我正在开发一款游戏,但我认为我遇到了范围问题...
它从我的 js 文件开始,通过点击玩家想要玩的卡触发:
It starts in my js file:
function playCard(playerId, card){
$.post("support_files/Blades_functions.php", {
method:'playCardFromHand',
playerID:playerId,
cardName:card
},function(data){
var attr = new Array();
attr = data.split("/");
alert(data);
$("#playerAttackPower").html(attr[0]);
$("#playerDefensePower").html(attr[1]);
$("#playerHealth").html(attr[2]);
$("#playerAttackMod").html(attr[3]);
$("#playerChargeCounters").html(attr[4]);
$("#playerSpecialAttackCounters").html(attr[5]);
});
}
Then into my php file
function playCardFromHand(){
$weapons = new WeaponCards(); // this is basically a class with variables(card objects) (my answer to php not having enums really)
$player = $theGame->gamePlayers[$_POST['playerID']]; // this is the variable ($theGame) that I declare in my main php file with all the markup on it. I'm including my functions file in my main file so the variable should be visible... I've tried using "global" keyword as well and that didn't do anything different...
$cardName = $_POST['cardName'];
$card = $weapons->$cardName;
$card->applyCard($player);
echo $player->getAttack()."/".$player->getDefense()."/".$player->getLife()."/".$player->getAttackMultiplier()."/".$player->getChargeCounters()."/".$player->getSpecAttackCounters();
}
所以我的问题是 $player 变量为空,所以什么都没有显示。基本上,正如您在 js 函数的回调中看到的那样,我只是解析字符串并更新主 php 页面上的播放器统计信息。那么这是一个范围的事情吗?有没有办法让 PHP 持久化 $theGame 变量,它本身就是一个对象,以便我可以在我的函数文件中访问/修改它?
任何想法都会很有帮助...正如我所说,我尝试使用 global 关键字...尝试将 $theGame 对象中的变量公开,尝试将它们设为私有并使用 getter 和 setter...我已经实际上有一个单独的问题,似乎无法让我的函数在我的对象上工作,只是将我的变量公开并直接访问它们,而不是使用 getter 和 setter。反正。请帮忙!我好沮丧!!!哈哈
谢谢,乔恩