2015-03-31 115 views
1

这将是一个很奇怪的问题,但请耐心等待。我的PHP阵列问题

我正在编写一个基于浏览器的游戏,每个玩家都有一定数量的卫兵,每人有100个卫生。每次射击时,卫兵失去健康。如果所有的守卫都死了,玩家就会改变健康。

射门伤害总是重叠,所以如果球员有3名后卫,而顶级后卫有60名生命,则100次射门会杀死后卫3并留下60后卫2。

我使用一个php数组来排序这个,它起作用,除非它涉及到玩家的健康。它不能正确计算,例如所有的球员守卫都已经死亡,并且球员剩下60个生命值。他被射中了100,而不是死于健康循环,所以他有一些其他数字健康而不是-40。

$p_bg = array(); // players guard count 
$p_bg[0] = $rs[bgs_hp2]; // only the top guard hp is saved (bgs_hp2) 
$p_hp = $rs[hp2]; // players health 
$dmg = 80 // shot damage 

$x = 0; 
while($x < $rs[BGs2]) { $p_bg[$x] = 100; $x++; } 

// As long as there's still damage to take and bgs to take it: 
while($dmg && !empty($p_bg)) { 
    $soak = min($p_bg[0], $dmg); // not more than the first bg can take 
    $p_bg[0] -= $soak; // remove hps from the first bg 
    $dmg -= $soak; // deduct from the amount of damage to tage 

    if ($p_bg[0] == 0) { 
     // bodyguard dead, remove him from the array 
     array_shift($p_bg); 
     } 
    } 

// If there's any damage left over, it goes to hp 
$p_hp = $p_hp - $dmg; 
+0

我现在不在PHP框中,所以不能为你做,但这是'xdebug'的错误。安装它来遍历你的代码,它会告诉你变量在哪里被改变。 – 2015-03-31 20:39:57

回答

0

不知道的$rs内容或知道什么是常量bgs_hp2hp2BGs2是,这是很难说,但它似乎是问题的关键在于围绕线的结合:

$p_bg = array(); // Create an empty array 
$p_bg[0] = $rs[bgs_hp2]; // Populate the first index, possibly null? 
$x = 0; 
while($x < $rs[BGs2]) { $p_bg[$x] = 100; $x++; } 

我会怀疑BGs2是玩家的保镖吗?每次这个代码被击中时,你似乎将最高保镖的健康度设置为100。

$p_bg = array($rs[bgs_hp2]); 
for ($x = 0; $x < $rs[BGs2]; $x++) { $p_bg[] = 100; } 

其他,注销你的变量(使用print_r($var)var_dump($var)必要的),看你执行上的实际数据:也许,如果它被重新安排如下将更为清晰。祝你好运!

+0

我认为这可能是因为我没有设定当前的保镖健康。谢谢 – 2015-03-31 21:20:57

+0

'BGs2'应该在引号之间。 – 2015-03-31 21:30:37