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

2013/4/3

How to take multiple hits?

OxiClean OxiClean

2013/4/3

#
Currently I am using this simple code:
    public void Shot()
    {
        Actor TheBoss1;
        TheBoss1 = getOneObjectAtOffset(0, 0, Bullet.class);
        if (TheBoss1 != null)
        {
            World world;
            world = getWorld();
            world.removeObject(this);
        }
    }
Obviously when this Actor (TheBoss1) touches the Bullet, it removes itself. I want to be able to define a number of Bullets that it gets hit by and THEN removes itself. Could anyone please be so kind as to helping me? Thank you!!!
danpost danpost

2013/4/3

#
The way you explained it is not the way the code shows it. You are setting 'TheBoss1' as a 'Bullet' object; this means that 'this Actor (TheBoss1) touches the Bullet' is the same as a bullet touching a bullet. I am sure that is not what you meant. Maybe you meant:
public void Shot()
{
    Actor bullet;
    bullet = getOneObjectAtOffset(0, 0, Bullet.class);
    if (bullet != null) getWorld().removeObject(this);
}
This does not yet fix your issue of having getting several bullet hits before dying. What you need is an instance int field for 'health' or 'lives'. Assuming TheBoss1 class is the class whose object is being hit by the bullet and loosing health, that is where to add this instance field. Also, removing the bullet would be a good idea so that the value of this field does not repeatedly decrease with the same bullet.
// add the instance field
private int health = 3;
// then in the same class
public void Shot()
{
    Actor bullet = getOneObjectAtOffset(0, 0, Bullet.class);
    if (bullet != null)
    {
        getWorld().removeObject(bullet);
        health--;
        if (health == 0) getWorld().removeObject(this);
}
OxiClean OxiClean

2013/4/3

#
Thank you danpost, you've helped me on several issues, when I upload this project I'll make sure to give you some deserved credit!
You need to login to post a reply.