This site requires JavaScript, please enable it in your browser!
Greenfoot back
CSBossmann
CSBossmann wrote ...

2015/3/7

How can I stop an actor for a specific time?

CSBossmann CSBossmann

2015/3/7

#
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?
danpost danpost

2015/3/7

#
You just need an int field to act as a 'stunTimer'. When hit by a bullet, set its value to something around 200 to 300. In the act method, if it is not zero, decrement it, otherwise move the character.
CSBossmann CSBossmann

2015/3/7

#
Ok thanks! Could you give me a code example? Because I am very new to Greenfoot and I don´t really know how I should convert your advice into code :D Thank you!
danpost danpost

2015/3/7

#
In the class of you character (the one being hit by the bullets):
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();
Put movement code in a 'private void move()' method.
CSBossmann CSBossmann

2015/3/7

#
Ok, I copied your code but somwehow nothing happens, if the Bullet hits the actor... Here is my code. Do you know were I made the mistake? Thank you for your help!
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());
        }
    }
  }
danpost danpost

2015/3/7

#
Remove the semi-colon, ';', at the end of line 21. By itself, it is considered a null statement and completes the 'if' block. So, line 22 ends up outside the 'if' block, which is not what you want. You probably do not want the troll to do anything when stunned. So, remove line 23 and insert the following at the beginning of the act method (line 13):
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
}
CSBossmann CSBossmann

2015/3/9

#
Thank you!
You need to login to post a reply.