I have been trying to make a health function in this scenario. I got it all to work, except that I can't make an explosion occur when the rocket has been hit 5 times.
Here is the code I can't get to execute (due to NullPointerException error):
This is the asteroid class. And the line that makes the error is line 12. I don't understand why the first if statement works, but the second one doesn't.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 | Actor rocket = getOneIntersectingObject(Rocket. class ); if (rocket != null ) { if (((Galaxy) getWorld()).hits < 5 ) { Galaxy world = (Galaxy)getWorld(); world.updateHealth(); Greenfoot.playSound( "crunch.mp3" ); getWorld().removeObject( this ); } else if (((Galaxy) getWorld()).hits == 5 ) { getWorld().addObject( new Explosion(), getX(), getY()); getWorld().removeObject(rocket); getWorld().removeObject( this ); Galaxy world = (Galaxy)getWorld(); world.updateHealth(); } } |