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

2017/1/30

timer

gergoa12 gergoa12

2017/1/30

#
i need a timer to count down abilities...for example a jump,or a shot..i want in in every 3second...can somebody help my?(sorry for bad english)
Super_Hippo Super_Hippo

2017/1/31

#
Create an int field which you can use as a timer:
private int shotTimer = 0;
Whenever you shoot, set the timer to some value (3 seconds is about 170 I guess).
shotTimer = 170;
Each act cycle, you reduce the value by 1 if it isn't 0:
if (shotTimer > 0) shotTimer--;
Before shooting, check if the shotTimer is 0, you can also use it right after the last line, so:
if (shotTimer > 0) shotTimer--;
else if (Greenfoot.isKeyDown("space"))
{
    shotTimer = 170;
    //shoot the bullet
}
You need to login to post a reply.