Hi,
Im trying to make a Greenfoot game. But I don´t understand how I can stop one of my actors for like 5 seconds, if he got hit by a Bullet.
Anybody who can help me?


1 2 3 4 5 6 | // add the following instance field (outside any method block, but inside the class block) private int stunTimer; // in the block where you determine the actor was hit by a bullet, add this line stunTimer = 200 ; // adjust value as needed // in the act method, add the following lines if (stunTimer > 0 ) stunTimer--; else move(); |
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 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 | public class SchweinchenBopps extends Troll { private int timer = 0 ; private Counter2 counter2; private int stunTimer; public SchweinchenBopps (Counter2 pointCounter2) { counter2 = pointCounter2; } public void act() { checkKeypress(); lookForRobbe(); shoot(); lookForBulletH(); } public void lookForBulletH () { if ( canSee(BulletH. class )); stunTimer = 1000 ; if (stunTimer > 0 ) stunTimer--; else move(); } private void checkKeypress() { if (Greenfoot.isKeyDown( "left" )) { turn(- 4 ); } if (Greenfoot.isKeyDown( "right" )) { turn( 4 ); } if (Greenfoot.isKeyDown( "up" )) { move( 4 ); } if (Greenfoot.isKeyDown( "down" )) { move(- 4 ); } } public void lookForRobbe () { if ( canSee(Robbe. class ) ) { eat(Robbe. class ); counter2.add( 5 ); } } public void shoot () { if (timer> 0 )timer--; if (timer== 0 && Greenfoot.isKeyDown( "space" )) { BulletS bullets= new BulletS(); getWorld().addObject(bullets, getX()+ 25 , getY()); timer= 60 ; bullets.setRotation(getRotation()); } } } |
1 2 3 4 5 | if (stunTimer > 0 ) { stunTimer--; return ; // this forces an immediate exit from the method so no further code within the method is executed } |