I have this code in a class that calls a method in the Hearts class once when it is touching Bob
The method below changes the amount of lives...
Town town = (Town)getWorld();
public void bobLives(int num)
{
lives += num;
if(lives>=6)
{
setImage("heartsfull.png");
}
if(lives==5)
{
setImage("2hearts1half.png");
}
if(lives==4)
{
setImage("2hearts.png");
}
if(lives==3)
{
setImage("1heart1half.png");
}
if(lives==2)
{
setImage("1heart.png");
Greenfoot.playSound("lowhealthmusic.wav");
}
if(lives==1)
{
setImage("halfheart.png");
}
if(lives<=0)
{
setImage("nohearts.png");
Greenfoot.setWorld(new GameOver());
Greenfoot.playSound("jingles_NES15.wav");
Greenfoot.delay(12);
Greenfoot.playSound("jingles_NES15.wav");
Greenfoot.delay(12);
Greenfoot.playSound("jingles_NES15.wav");
}
}
...but whenever I run the method I get
I've been trying a lot of different things, none of them working, so it would be great if someone could help me
boolean isTouchingBob = false; Town town = (Town)getWorld(); public void act() { damageBob(); } public void damageBob() { if(isTouching(Bob.class)) { if(isTouchingBob == false) { town.returnHearts().bobLives(-1); isTouchingBob = true; } else { isTouchingBob = false; } } }
java.lang.NullPointerException at Damage.damageBob(Damage.java:33) at Damage.act(Damage.java:20) at greenfoot.core.Simulation.actActor(Simulation.java:594) at greenfoot.core.Simulation.runOneLoop(Simulation.java:552) at greenfoot.core.Simulation.runContent(Simulation.java:215) at greenfoot.core.Simulation.run(Simulation.java:205)