View Full Version : help with health in a game

dark acalyte
11-06-2007, 10:58 PM
var health = 100;

onClipEvent (enterFrame) {
for (i=0; i<_root.nrEnemies && _root.health>0; i++) {
if (this.hitTest(_root["Enemy"+i])) {
_root.health -= random(11)+5;
if (_root.health<=0) {
_root.health = 0;
_root.lives -= 1;
for (k=0; k<_root.nrEnemies; k++) {

im working on a game where the health starts at 100 and goes to 0 through various hits from an enemy that spews out random damage ranging from 5-12; i had a problem earlier where when the health reached zero and i was still hitting the enemy my health would go past 0 into the negatives, but i fixed that and it stops at 0 because when the health >0 the health stays at 0 till the maincharacter movieclip resets. my problem is is that when the maincharacter's health reaches exactly 0 ( as in the enemies hits that are random 5-12 add up to exactly zero and not negative 1) he respawns invincible and isnt affected by the enemies anymore, the above code is what deals with the health, any help would be appreciated.

11-06-2007, 11:48 PM

I suspect the problem is with this: && _root.health>0

...in your for loop. If you're health is not greater than zero, then the for-loop will may not execute at all. I've actually never tried putting a boolean value (_root.health>0) in as a limit for a loop but without trying it, at a glance this looks suspect.

Although there may be a more eligant way to do this, I would perform your if-stuff first, then if you're health is greater than zero, loop through your foes and check for damage.

just my 2 cents...