Hi, I'm trying to "do" something that i'm not sure if i am allowed to do or not...
Here are some codes:
This is my Drop class:
And there's 2 other classes: Heal and Credit that extend the Drop class (they're empty)
And then I've a method in my Monster class as following:
The code compiles, but when i kill a monster, there's a java error, which tell me null pointer exception...
The idea behind this code is that when a monster dies, I want to have 50% chance to have a Heal and 50% chance to have a Credit
Could you tell me what's going wrong in my code please?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 | import greenfoot.*; public class Drop extends SmoothMover { public Drop() { } public Drop( int x, int y) { if (Greenfoot.getRandomNumber( 100 ) < 50 ) { Credit credit = new Credit(); System.out.println(credit); getWorld().addObject(credit,x ,y ); } else if (Greenfoot.getRandomNumber( 100 ) >= 50 ) { Heal heal = new Heal(); getWorld().addObject(heal,x , y); } } public void act() { } } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | public void takeDamageNew( int hurt, Class classe) { if (getWorld() != null ) { if ( this .isTouching(classe)) { Life = Life - hurt; if (Life <= 0 ) { getWorld().addObject( new Drop( this .getX(), this .getY()), this .getX(), this .getY()); getWorld().removeObject( this ); Game.killedEnemies++; } } } } |