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

2019/4/2

Create Timer

RedMine360 RedMine360

2019/4/2

#
Hey i want to create a methode that pauses for some seconds but isn't stopping the act(). What I tried now was
1
2
3
4
5
6
7
8
public void timer(int delay)
    {
        delay = delay * 100000/2;// to get a higher timer (now 1 nearly 1 sec)
        for(int t = delay; t != 0; t--)
        {
            System.out.println(t);// to see how long the timer is
        }
    }
the probleme here is at higher numbers it gets stuck at some point for a few seconds and the problem I had with Greenfoot.delay() is that it stops the complete act() for this time. Any solutions? I have a programm where u shoot arrows and you can only shoot another arrow if the first is gone but i want just a one sec timer instead. Programm The Probleme is if u get close the enemy is dead way to fast.
danpost danpost

2019/4/2

#
Add a int field for a shot delay timer:
1
private int shotDelay;
Then count act cycles with it. Normally, 60 act cycles will process in about 1 second of time. So, shoot an arrow when the timer is zero and set the delay to 60 when shooting an arrow:
1
2
3
4
5
6
7
8
if (shotDelay > 0) --shotDelay; else
{
    if (/** condition to shoot arrow here */)
    {
        /** shoot arrow here */
        shotDelay = 60;
    }
}
RedMine360 RedMine360

2019/4/2

#
Nice thx and the information about the 60 act cycles is also very usefull.
You need to login to post a reply.