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 32 33 34 35 36 37 38 | public void act() { setImage( new GreenfootImage( 102 , 52 )); getImage().drawRect( 0 , 0 , 51 , 11 ); getImage().setColor(Color.RED); getImage().fillRect( 1 , 1 ,health, 10 ); World world = getWorld(); if (getWorld() instanceof MyWorld){ loseHealthMyWorld(); } if (getWorld() instanceof HardRoom){ loseHealthMyWorld(); } } public void loseHealthMyWorld() { if (myWorld.getPlayer().hitByEnemy() == true ) { health--; } else health = health; if (health<= 0 ) { Greenfoot.stop(); } } public void loseHealthHardRoom() { if (hardRoom.getPlayer().hitByEnemy()) { health--; } if (health<= 0 ) { Greenfoot.stop(); } } |
1 2 3 4 5 6 7 8 9 10 11 | public boolean hitByEnemy() { Actor enemy = getOneObjectAtOffset( 0 , 0 , ENEMY. class ); Actor slime = getOneObjectAtOffset( 0 , 0 , Slime. class ); if (enemy!= null || slime!= null ) { return true ; } else return false ; } |