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

2012/6/26

boss health?

davemib123 davemib123

2012/6/26

#
Hi All, I am trying to implement a boss in my game, but for the life of me I can not figure how to kill it. So far I have this:
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class miniBoss here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class miniBoss extends Enemies
{
    private int Shield;
    private int Score;
    private int life;

    protected void takeLife() {
        this.life -= 1;
        if (this.life <= 0)
            getWorld().removeObject(this);
    }

    /**
     * Act - do whatever the miniBoss wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act() 
    {
        this.Score = 10;
        this.life = 2;
        checkCollision();
    }    

    public void checkCollision()
    {
        Actor collided = getOneIntersectingObject(shipMissile.class);
        if (collided != null)
        {
           ((City) getWorld()).addScore(20);
            getWorld().addObject(new explosionLarge(), getX(), getY());
            getWorld().removeObject(collided);
            takeLife();
        }

    }

}
What do you guys think?
danpost danpost

2012/6/26

#
The act method is repeatedly called, once every cycle. If you 'set' variables within it, they will always be resetting. The 'Score' will always be reset to 10 and the 'life' to 2. Those two statements could be placed in the constructor for the object, which you apparently do not have, yet. However just to set variables, you really do not need a constructor. Here is what you can do: removes lines 27 and 28; and change lines 12 and 13 to the following:
private int Score = 10;
private int life = 2;
davemib123 davemib123

2012/6/27

#
code stands as this so far:
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class miniBoss here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class miniBoss extends Enemies
{
    private int Score = 500;
    private int life = 50;

    protected void takeLife() {
        life = life - 1;
        if (life <= 0)
            getWorld().removeObject(this);
    }

    /**
     * Act - do whatever the miniBoss wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act() 
    {
        checkCollision();
    }    

    public void checkCollision()
    {
        Actor collided = getOneIntersectingObject(shipMissile.class);
        if (collided != null)
        {
            ((City) getWorld()).addScore(20);
            getWorld().addObject(new explosionLarge(), getX(), getY());
            takeLife();
            getWorld().removeObject(collided); 
        }

    }

}
what is the next step? As the missile makes an impact and I see the explosion but the life is not taken away.
davemib123 davemib123

2012/6/27

#
got it sorted :) thanks for the tips
danpost danpost

2012/6/27

#
You will get a run-time error when the miniBoss dies. In the 'takeLife' method, the miniBoss is removed from the world when he dies; but, then, you call 'getWorld' in the next statement, which will then return 'null', and trigger a NullPointerException. Easy fix: switch lines 36 and 37 (remove 'collided' then 'takeLife'). EDIT: I do not know what prompted you to, but 13 hours ago, it appears they were in the correct order. But, 2 hours ago, you posted the same code with those statements switched.
davemib123 davemib123

2012/6/27

#
yea I figured that when I was testing it. thanks for the tips :)
You need to login to post a reply.