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

2011/12/20

timer

milanapa milanapa

2011/12/20

#
hello. i want to use timer to end the games. anyone know?
InuoeK InuoeK

2011/12/20

#
I'd do it like this, although its not directly accurate to each second
private int timer=0;

public void act(){
addTimer();

if (timer == (insert end time here)){
    Greenfoot.Stop();
   }
}

private void addTimer(){
     timer++;
}
mik mik

2011/12/20

#
You're right, InuoeK, it's not accurate in seconds, but it's probably the best way to do it. Using a counter for timing, as you did (as opposed to working with real time), is much simpler, and almost always what you want. This way, you are measuring "game time" rather than real time, and that often makes more sense.
milanapa milanapa

2011/12/20

#
the coding that you post, where should i put it? In actor or world?
lonely.girl lonely.girl

2013/1/8

#
Hello there just wanna say I used the code above in the game but it didn't stop I put in the world but nothing is happening any help??
Gevater_Tod4711 Gevater_Tod4711

2013/1/8

#
If you could post your source code we could easyer help you.
danpost danpost

2013/1/8

#
@milanapa, it would make sense to put the code in the class that creates the object that you are timing (if you are timing how long a world is active, put it in the class of that world; if you are timing how long an actor is 'alive', put it in the class of that actor). That way, as you add additional code, there will be less chance of having to modify this part.
You need to login to post a reply.