Hi everybody, in my game I am trying to have hearts to represent my health in the top right corner. If a zombie touches you, it makes the heart gray, I use 8 different hearts for classes and place them on the screen, the main actor moves along with these hearts through all the worlds and I used setImage() to make it gray, here is the code:
whenever the actor moves to a new world I make the hearts reappear in the same places and of course they turn gray since the if (shooter.hp < 1) is true, it gets that from the main actor ShotgunSprite (or shooter), I have managed to do all of but here comes the error, whenever I set a new world, the game stops and gives the error below; If I press run again it works fine again with gray hearts and all, how can I get it to not do this? From what I can understand it has something to do with the getObjects part.
java.lang.IndexOutOfBoundsException: Index: 0, Size: 0
at java.util.ArrayList.RangeCheck(ArrayList.java:547)
at java.util.ArrayList.get(ArrayList.java:322)
at Heart4.heart(Heart4.java:22)
at Heart4.act(Heart4.java:17)
at greenfoot.core.Simulation.actActor(Simulation.java:498)
at greenfoot.core.Simulation.runOneLoop(Simulation.java:461)
at greenfoot.core.Simulation.runContent(Simulation.java:197)
at greenfoot.core.Simulation.run(Simulation.java:187)
public class Heart1 extends Hearts
{
/**
* Act - do whatever the Heart wants to do. This method is called whenever
* the 'Act' or 'Run' button gets pressed in the environment.
*/
public void act()
{
heart();
}
public void heart()
{
ShotgunSprite shooter = (ShotgunSprite) getWorld().getObjects(ShotgunSprite.class).get(0);
if (shooter.hp < 1)
{
setImage("grayheart.png");
}
if (shooter.hp >= 1)
{
setImage("heart.png");
}
}
}
