Hi,
In my game I want a counter to increase everytime an alien is getting killed. In order to do that I imported the Counter subclass and wrote the following in my world subclass to spawn it:
In the Alien-Subclass (checkHealth is also in the Act-Method)
Sadly I'm getting the following error while hitting the Alien and setting its health to 0 and below:
I'm quite new to Greenfoot and programming at all, so it'd be nice if someone knew how to handle to error.
Thanks.
public class space extends World { /** * Constructor for objects of class space. * */ private Counter theCounter; public space() { // Create a new world with 600x400 cells with a cell size of 1x1 pixels. super(600, 400, 1); theCounter = new Counter(); addObject(theCounter, 60, 360); } public Counter getCounter() { return theCounter; } }
public void changeCounter(int score) { space spaceWorld = (space) getWorld(); Counter counter = spaceWorld.getCounter(); counter.add(score); } public void checkHealth() { Actor alien = getOneIntersectingObject(alien.class); if (health <= 0) { getWorld().removeObject(alien); getWorld().removeObject(this); changeCounter(1); } }
java.lang.NullPointerException at alien.changeCounter(alien.java:69) at alien.checkHealth(alien.java:80) at alien.act(alien.java:36) at greenfoot.core.Simulation.actActor(Simulation.java:568) at greenfoot.core.Simulation.runOneLoop(Simulation.java:526) at greenfoot.core.Simulation.runContent(Simulation.java:215) at greenfoot.core.Simulation.run(Simulation.java:205)