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

2018/7/25

how to put timer in a game in greenfoot version 2.4.2

liverpoolfc liverpoolfc

2018/7/25

#
Could you please help me create a timer of 60 seconds .
Agent40 Agent40

2018/7/25

#
60 ticks occur in a second within Greenfoot btw (just for future reference).
// In Constructor 
private boolean TimerStart = false; // used to start the timer, set off by another method... i.e. TimerStart = true; 
private int Timer 3600; // 60x60 (60 ticks, 60 seconds)
...

if (TimerStart == true)
{
      Timer--; // Or Timer = Timer - 1;
      if (Timer <= 0)
      {
            // Do whatever happens when the timer runs out here... or just remove it :P
      }
}


liverpoolfc liverpoolfc

2018/7/26

#
in which class should we put it
danpost danpost

2018/7/26

#
liverpoolfc wrote...
in which class should we put it
For a game timer, your World subclass would be the appropriate place. The timer system does not require a boolean (TimerStart) to work:
// field
private int countdown = 3600;

// in act
if (countdown > 0 && --countdown == 0)
{
    // game over code here
}
liverpoolfc liverpoolfc

2018/7/26

#
I want the user to see the timer also
liverpoolfc liverpoolfc

2018/7/26

#
I want the timer to be in real time also.
danpost danpost

2018/7/26

#
liverpoolfc wrote...
I want the user to see the timer also
Please refer to my Value Display Tutorial scenario.
danpost danpost

2018/7/26

#
liverpoolfc wrote...
I want the timer to be in real time also.
You get better consistency when counting act cycles.
liverpoolfc liverpoolfc

2018/7/26

#
I didn't understand
liverpoolfc liverpoolfc

2018/7/26

#
When I wrote private int countdown = 3600; , it showed illegal start of an expression.
liverpoolfc liverpoolfc

2018/7/26

#
In the Value Display Tutorial which tutorial should I click?
Agent40 Agent40

2018/7/27

#
danpost wrote...
liverpoolfc wrote...
in which class should we put it
For a game timer, your World subclass would be the appropriate place. The timer system does not require a boolean (TimerStart) to work:
// field
private int countdown = 3600;

// in act
if (countdown > 0 && --countdown == 0)
{
    // game over code here
}
of-course a timer system within code doesn't require boolean value to activate, however it is considerably useful as it allows for other methods and conditions to activate it instead of being instantaneous. That said calling upon a method has the same effect.
Super_Hippo Super_Hippo

2018/7/27

#
liverpoolfc wrote...
When I wrote private int countdown = 3600; , it showed illegal start of an expression.
It should be at the top of the class outside any methods.
You need to login to post a reply.