My PHP array issue -


this pretty strange question, bear me.

i'm coding browser based game, , each player has amount of guards, each 100 health. every time shot, guards lose health. if guards dead, player takes health instead.

the shot damage overlaps well, if player has 3 guards, , top guard has 60 health, shot of 100 kill guard 3 , leave guard 2 60 health.

i use php array sort this, , works, except when comes down players health. doesn't calculate correctly, eg players guards dead , player has 60 health left. shot 100, instead of dying health loops round has other number health instead of -40.

$p_bg = array(); // players guard count $p_bg[0] = $rs[bgs_hp2]; // top guard hp saved (bgs_hp2) $p_hp = $rs[hp2]; // players health $dmg = 80 // shot damage  $x = 0; while($x < $rs[bgs2]) { $p_bg[$x] = 100; $x++; }   // long there's still damage take , bgs take it: while($dmg && !empty($p_bg)) {    $soak = min($p_bg[0], $dmg); // not more first bg can take    $p_bg[0] -= $soak; // remove hps first bg    $dmg -= $soak; // deduct amount of damage tage     if ($p_bg[0] == 0) {       // bodyguard dead, remove him array       array_shift($p_bg);       }    }  // if there's damage left over, goes hp $p_hp = $p_hp - $dmg; 

without knowing contents of $rs or knowing constants bgs_hp2, hp2, , bgs2 are, it's hard say, seems problem lies around combination of lines:

$p_bg = array(); // create empty array $p_bg[0] = $rs[bgs_hp2]; // populate first index, possibly null? $x = 0; while($x < $rs[bgs2]) { $p_bg[$x] = 100; $x++; } 

i'd suspect bgs2 bodyguard count player? seems you're setting top bodyguard's health 100 each time code hit. maybe more clear if rearranged follows:

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

other that, log out variables (using print_r($var) or var_dump($var) necessary) see actual data you're executing on. luck!


Comments

Popular posts from this blog

cakephp - simple blog with croogo -

How to group boxplot outliers in gnuplot -

bash - Performing variable substitution in a string -