Im gave my ship 3 lives but since i wanted a smaller image i decided to make another actor with the same image but alot smaller and depending on how many lives you have the left hand corner of the game will show how many ships you have remaining and once you reach 0 you wont come back. Problem is i keep getting a error:
java.lang.NullPointerException
at BadLaser.destroy(BadLaser.java:68)
at BadLaser.act(BadLaser.java:30)
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)
the class that is accessing the health task is my BadLaser:
public void destroy()
{
spaceShip ship = (spaceShip) getOneIntersectingObject(spaceShip.class);
lives life = (lives) getOneIntersectingObject(lives.class);
GreenfootSound explodeAlien = new GreenfootSound("Explode.wav");
explodeAlien.setVolume(75);
if(ship != null)
{
unlock = true;
explodeAlien.play();
getWorld().removeObject(ship);
getWorld().removeObject(this);
life.losehealth(1);
life.reset();
}
}
the life class itself is:
private int health = 3;
public lives()
{
GreenfootImage image = getImage();
image.scale(image.getWidth() - 1150, image.getHeight()-1150);
setImage(image);
setRotation(0);
}
public void losehealth(int loss)
{
health=health-loss;
}
public int getHealth()
{
return health;
}
public void reset()
{
spaceShip ship = new spaceShip();
getWorld().addObject(ship, 311,500);
}
I've also already placed the images depicting how many health you have left in my world class.
I feel like this isnt the most efficient way of creating a health system so if someone has a better idea on how to do it im all ears, please help!