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

2013/12/9

Zombie game

jubyfollower jubyfollower

2013/12/9

#
okay so I've worked on my zombie game and I'm trying to figure out the code so that the zombie takes damage and disappear from the game, so far I've got the bullets when they hit the zombie disappear, I've looked at other zombies games but quite cant figure out how to type the code in and make it work,
danpost danpost

2013/12/9

#
Basically, you need an instance 'int' field in the Zombie class to track the health (points) of the zombies and a method to change it value (call the field 'health' and name the method 'adjustHealth'). Something like this
1
2
3
4
5
6
7
8
9
// ***** Zombie class ****
// instance field
private int health = 100; // whatever starting value
// method
public void adjustHealth(int adjustment)
{
    health += adjustment;
    if (health <=0) getWorld().removeObject(this);
}
Now, the collision between bullet and zombie can be done in either class; but since the zombies receive multiple hits and has the health field, it might be easiest to code the collision in the Zombie class.
1
2
3
4
5
6
7
8
9
public void checkHit()
{
    Actor bullet = getOneIntersectingObject(Bullet.class);
    if (bullet != null)
    {
        getWorld().removeObject(bullet);
        adjustHealh(-20); // whatever value
    }
}
Then, it is just a matter of calling 'checkHit' from the 'act' method.
jubyfollower jubyfollower

2013/12/10

#
Alright Thanks :D i got it working!
You need to login to post a reply.