Hello, I made 2 actor classes for the 2 levels I made (a first level and a boss level) I also made 2 hearts classes to represent the lives of the 2 characters. The code for the hearts class is this:
When an enemy such as an eye or a fireball touches either of the characters, it will send an adjustment value to their respective Hearts classes. The code for the contact between the fireball and the character is this:
The first level progresses smoothly, the heart images cycle at the lower left portion of the screen and it loads up to the next level, but when the character hits a fireball shot by the boss monster, the game stops and this error appears:
import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
public class Hearts extends Actor
{
//arraylist to cycle between the hearts of the knight
GreenfootImage[] hearts = { new GreenfootImage("healthbar0.png"),
new GreenfootImage("healthbar1.png"),
new GreenfootImage("healthbar2.png"),
new GreenfootImage("healthbar3.png")};
int health = 3;
public void act()
{
// Add your action code here.
}
public Hearts()
{
//method used to call on update image
updateImage();
}
private void updateImage()
{
//sets # of hearts depending on what image the arraylist holds
setImage(hearts[health]);
}
public void adjustHearts(int adjustmentValue)
{
//conditions for the hearts, makes the images cycle between themselves, calls on the DEAD banner and stops game on 0 hearts
health += adjustmentValue;
if (health > 3)
{
health = 3;
}
if (health == 0)
{
getWorld().addObject(new DEAD(this), 400, 300);
updateImage();
Greenfoot.stop();
}
else
{
updateImage();
}
}
}
public void burn(){
//method to damage KNIGHT on contact with character
if (isTouching(KNIGHT.class)){
MyWorld worldsubclass = (MyWorld) getWorld();
worldsubclass.hearts.adjustHearts(-1);
}
if (isTouching(KNIGHT2.class)){
BossLevel bosslevel = (BossLevel) getWorld();
bosslevel.hearts2.adjustHearts(-1);
}
}java.lang.NullPointerException
at Hearts2.adjustHearts(Hearts2.java:45)
at FIREBALLl.burn(FIREBALLl.java:53)
at FIREBALLl.act(FIREBALLl.java:23)
Help is very much appreciated!