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

2013/5/27

Creating health for an object

Neon147 Neon147

2013/5/27

#
I'm trying to lower the enemy's health everytime the laser hits it but i keep getting an error: java.lang.NullPointerException at Laser.destroy(Laser.java:120) at Laser.act(Laser.java:33) at greenfoot.core.Simulation.actActor(Simulation.java:565) at greenfoot.core.Simulation.runOneLoop(Simulation.java:523) at greenfoot.core.Simulation.runContent(Simulation.java:213) at greenfoot.core.Simulation.run(Simulation.java:203) my laser class is this: Actor BossWall = getOneIntersectingObject(BossWall.class); public void destroy() { if(BossWall != null) { BossWall wall = (BossWall) getOneObjectAtOffset(0, 0, BossWall.class); unlock = true; explodeAlien.play(); if(wall.checkH()==0) { getWorld().removeObject(BossWall); } wall.health(-1); getWorld().removeObject(this); } } Can i get some help please? I think its the way im using my wall.checkH() method is an accessor so i assumed that how it should look
danpost danpost

2013/5/27

#
You are declaring the 'destroy' method AFTER the first line of its code. Move the line above the method declaration down inside the method.
Neon147 Neon147

2013/5/27

#
Sorry i don't understand what you mean by the line
danpost danpost

2013/5/27

#
You have 'Actor BossWall = getOneIntersectingObject(BossWall.class);' outside (above) the 'destroy' method, where it should be the first statement within the method. Even then you may still error, as you are using two different collision checks. The object could be detected in the first and not detected in the second, causing 'wall' to be 'null' and erring at 'wall.checkH'. Re-write the method to the following:
public void destroy()
{
    BossWall bosswall = (BossWall) getOneIntersectingObject(BossWall.class);
    if(bosswall != null)
    {
        unlock = true;
        explodeAlien.play();
        bosswall.health(-1);
        if(bosswall.checkH()==0)
        {
            getWorld().removeObject(bosswall);
        }
        getWorld().removeObject(this); 
     }
}
Neon147 Neon147

2013/5/27

#
Okay thanks! No more error now, but now the boss wall isnt disappearing, so would that be my bosswall class causing it? Its: private int health=10; public BossWall() { GreenfootImage image = getImage(); image.scale(image.getWidth() - 350, image.getHeight()-105); } public void act() { move(1); if(getX() == 1) { setRotation(0); } if(getX() == getWorld().getWidth()-1) { setRotation(180); } } public void health(int loss) { health=health-loss; } public int checkH() { return health; }
danpost danpost

2013/5/28

#
If you had named your method a little better, you would have avoided causing this error. Your 'health' method subtracts the value given in the argument and you are sending it a negative number. Subtracting a negative number is like adding its inverse. Your health value is increasing, not decreasing; so, it never gets to zero. Change the name of the method to 'loseHealth' and call it in your Laser class with 'bosswall.loseHealth(1);'
Neon147 Neon147

2013/5/28

#
Ahh yes i see that now, thanks ill change the name
You need to login to post a reply.