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

2011/10/5

timer

Zocker1996 Zocker1996

2011/10/5

#
i want a timer of 21 secounds. aftter the timer the reset(); from the world should be used: public void time() { ######TIMER backwarts From 21 SECONDS to 0###### if(timer == 0) { reset(); } }
Zocker1996 Zocker1996

2011/10/5

#
but the theard should not stop! it should be like waiting but without System.currentTimeMillis()!!!! THANK YOU ;)
nccb nccb

2011/10/6

#
What's wrong with currentTimeMillis()? That would have been my suggestion on how to do it. When you want to start timing, set a field long startTime to be the current time:
startTime = System.currentTimeMillis();
Then in your act method:
if (startTime != 0 && startTime <= System.currentTimeMillis() + 21000)
{
    startTime = 0;
    reset();
}
Setting the startTime back to zero (and adding a check against zero to the if-condition) makes sure it will only trigger once when the time's up.
limefortheworld limefortheworld

2011/10/11

#
nccb, for some reason what I think is that he personally wants to make a timer that fits his game: 21 second doesn't mean the same thing in a 60fps game as what it means in a 30 fps game. Personally, I'd say he will need to know what his fps is, so that we can get the necessary value.
limefortheworld limefortheworld

2011/10/11

#
nccb wrote...
What's wrong with currentTimeMillis()? That would have been my suggestion on how to do it. When you want to start timing, set a field long startTime to be the current time:
startTime = System.currentTimeMillis();
Then in your act method:
if (startTime != 0 && startTime <= System.currentTimeMillis() + 21000)
{
    startTime = 0;
    reset();
}
Setting the startTime back to zero (and adding a check against zero to the if-condition) makes sure it will only trigger once when the time's up.
More specifically, I should have said that currentTimeMillis() method disregards frame rate, one of the reason I think personally that he doesn't want to use that method (for example in a 30fps environment, 21 seconds would be 630 cycles, while in a 60fps one it is 1260 cycles). and @Zocker1996, you might want to search for the FPS class scenario in the gallery. If your scenario is not code-intensive (and thus not straining the system), you might get that FPS value that you want to get that tick value to use for your comparison, something like
starttime = 0;

//replace constant with FPS you've obtained from the FPS class * 21
public void time()
if(starttime >= constant)
{
    starttime = 0;
    reset();
}
You need to login to post a reply.