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?
// 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();
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());
}
}
}
if (stunTimer > 0)
{
stunTimer--;
return; // this forces an immediate exit from the method so no further code within the method is executed
}