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

2016/3/13

Wait Function

AMiscool AMiscool

2016/3/13

#
Hello everyone. How do you make an actor wait for about 3 seconds after shooting? I can have my actor shoot, but when I hold space bar, it keeps on shooting. I want to change that so I can only shoot once every 3 seconds. Reply me the code and any specifics please. Thanks, AMiscool
danpost danpost

2016/3/13

#
Do you want the user to press the key once for every shoot? or, do you want the user to be able to hold down the button while shots are fired with a 3 second delay between them?
AMiscool AMiscool

2016/3/13

#
danpost wrote...
Do you want the user to press the key once for every shoot? or, do you want the user to be able to hold down the button while shots are fired with a 3 second delay between them?
@danpost It doesn't really matter for me. I morely want the user to be able to hold down the button while shots are fired with a 3 second delay. But if you can't, then its fine with the other option.
danpost danpost

2016/3/13

#
AMiscool wrote...
@danpost It doesn't really matter for me. I morely want the user to be able to hold down the button while shots are fired with a 3 second delay. But if you can't, then its fine with the other option.
The you only need at an instance int field for a delay timer. When a shot is fired set it to like 150 or 200. In the act method you can ask if it is zero or not; when zero, shoot if key is pressed; if not zero, subtract one from it:
1
2
3
4
5
6
7
8
9
10
11
12
13
// instance field
private int shotDelay;
 
// in act (or method it calls)
if (shotDelay > 0) shotDelay--;
else
{
    if (Greenfoot.isKeyDown("space"))
    {
        shoot();
        shotDelay = 180;
    }
}
You need to login to post a reply.