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

2013/12/16

Making a timer, printing it out and then going to a world.

Doge Doge

2013/12/16

#
Well, as the title says, I want my game to start counting when I click play, and I want the timer to be printed out at the top corner. When the timer hits 1 minute, I want the game to stop. I have absolutely no idea how to go about doing this. The scenario is here So I want it that when the world normMode starts, it starts counting down to 1 minute, then goes to a different world.
danpost danpost

2013/12/16

#
I would use (1) a timer 'int' field (to track the interval between 'clicks' of the 'clock') and (2) an Actor object (Counter object) to display the 'clock'. You will also need a 'boolean' field for starting the clock (indicating 'normMode'). These fields should be in your world class with a reference to the Counter object. When normMode starts, set the boolean to 'true'. When it is 'true', increment the timer field and check it for a specific value (maybe 50 or 60). When at that specific value, reset it to zero and adjust the Counter object. When the Counter object is adjusted, re-check its value for 60 or 0 or whatever indicates that 60 time-units has elapsed. If so, change the world.
Doge Doge

2013/12/17

#
How would I increment the timer field? Thanks for the help.
danpost danpost

2013/12/17

#
All 'action' should be coded within the 'act' method. The actions can be broken down into individual parts put into methods that the 'act' method then calls. The timer is only ran if in 'normMode', so:
// in the 'act' method
runTimer();
// with a method
private void runTimer()
{
    if (!normMode) return;
    timer = timer+1; // or 'timer++;'
The rest of the method would check the value of the timer; increment the value of the Counter object if necessary, while resetting the timer; check the value of the Counter object and set the new world, if necessary.
You need to login to post a reply.