This site requires JavaScript, please enable it in your browser!
Greenfoot back
DPD4AU
DPD4AU wrote ...

2012/11/6

Health

DPD4AU DPD4AU

2012/11/6

#
I have coded for a user to be placed in the bottom center of the screen. Zombies swarm from each side at random. How do I go about making user health (3 hits from zombie then dead) and zombie health (1 hit from user and dead). Removing the zombies after 1 hit and ending the game for the user taking 3 hits. I am using 4 different images for the user. I'm using left and right arrow to turn flip an image back and forth (2 of the images), while also hitting space bar to punch an object in both directions (2 other images). So basically I need the punch to deal the damage to the zombies. Also how do i end the game after a certain amount of zombies are killed?
Gevater_Tod4711 Gevater_Tod4711

2012/11/6

#
For a user health you just need a variable in the user class (life or something). For every zombie hitting you the variable is counted down. Probably there is only one object from typ user (or however it's called) so the eayest way to realize that is to use static variables:
//in your user class;
private static int health = 3;

//some other stuff;

public static void setHealth(int points) {
    health += points;
}
public static int getHealth() {//not realy necessary but to complete it...;
    return health;
}




//in your zombie class;
public void act() {
    if (hitUser()) {//if you found the user or whenever you want to hit him;
        User.setHealth(-1);//instead of User you have to add the real classname;
        //after that you probably should remove the zombie. Otherwhile he'll hit you every act.
    }
}
To let the game end after some zombies are dead you should remove all the objects and add a winer screen or something like that.
You need to login to post a reply.