Game Development Community

Countdown Clock

by Matthew Medina · in Torque Game Builder · 01/13/2007 (12:35 am) · 3 replies

Hi, I was wondering how I could create a countdown clock, that would show both minutes and seconds. I tried searching for how to create one, and everything I found required me to have a TGE license.

If I wanted it to start at 10:00 and go down to 00:00, would I create one text control for the minutes and one for the seconds? Then have the second one go down every second, and once it got to 0 go back up to 60, and then do the same for the minute counter, except have it go down every minute?

That way seems like it might work, but I'm not sure. It probably wouldn't be the best way. So can anybody please tell me if there would be a more efficient way to make a countdown clock that shows both minutes and seconds, and then once it gets to 0 it's game over?

Thanks.

#1
01/15/2007 (8:25 pm)
Here's code I use, coupled with a GUI text object (label)
function updateGameTimer()
{
   // only update if we are not paused or done
   if (GameData.gameState != $GAME_STATE_PAUSED && !GameData.isGameTimeUp)
   {
      // get the time remaining
      GameData.elapsedGameTime++;
      %gameTimeRemaining = GameData.gameTimer - (GameData.elapsedGameTime * 1000);
      
      // is the game over?
      if (%gameTimeRemaining == 0)
         GameData.isGameTimeUp = true;
      
      // convert into seconds
      %secondsRemaining = %gameTimeRemaining / 1000;
      
      // extract the number of minutes
      if (%secondsRemaining > 59)
         %minutesRemaining = mFloor(%secondsRemaining / 60);
      
      // get remainder of seconds
      %secondsRemaining = %secondsRemaining - (%minutesRemaining * 60);
      
      // update the time, prepend a 0 if less than 10 seconds
      if (%secondsRemaining < 10)
         %secondsRemaining = "0" @ %secondsRemaining;
         
      lblGameTimer.setText(%minutesRemaining @ ":" @ %secondsRemaining);
   }

   // call back in a second
   if (GameData.isGameRunning && !GameData.isGameTimeUp)
      schedule(1000, 0, updateGameTimer);
	
} // end updateGameTimer
Pretty straightforward - I've used similar code in many applications. Let me know if you have any questions
#2
01/20/2007 (6:09 pm)
Thank you, that looks a lot better than what I have.

Here's what I did, I made four GUI Text Controls, one for every ten minutes, one for every single minute, and then the same for every ten seconds and one second. Then I had it so when the level starts, it starts the one second countdown, and then once those get to zero, it subtracts from the ten seconds and resets to nine, then when the ten seconds finally reach zero after a minute zero, it subtracts from the one minute. On every level I want it to countdown, I just have an object with "timerCountdown" class.

It's probably not very good, but it still works fine as far as I know.

function timerCountdown::onLevelLoaded(%this)
{
   %this.schedule(1000, updateoneSeconds);

   %this.schedule(1000, updatetenMinutes);

   moveMap.bindCmd(keyboard, "pause", "scoreStopper();", "doNothing();");

   //If zero then game is not paused
   $scoreStop = 0;
}

///Controls the game pausing///
function scoreStopper()
{
     if($scoreStop == 0)
     {
        $scoreStop = $scoreStop + 1;
     }
     else
     {
        $scoreStop = $scoreStop - 1;
     }

  %sg = sceneWindow2d.getSceneGraph();
  %sg.setScenePause(!%sg.getScenePause());
}

///Lowers every second, changes ten seconds
function timerCountdown::updateoneSeconds(%this)
{
     if($scoreStop == 1)
      {
      $onesecond = $onesecond - 0;
      onesecond.setText($onesecond);
      }
     else
     if($scoreStop == 0)
      {
      $onesecond = $onesecond - 1;
      onesecond.setText($onesecond);
      }

    %this.scoreSchedule = %this.schedule(1000, updateoneSeconds);

    onesecond();

    if ( $onesecond < 0 )
    {
      $onesecond = 9;
      onesecond.setText($onesecond);

      %this.updatetenSeconds();
    } 
}

function timerCountdown::updatetenSeconds(%this)
{
     if($scoreStop == 1)
      {
      $tensecond = $tensecond - 0;
      tensecond.setText($tensecond);
      }
     else
     if($scoreStop == 0)
      {
      $tensecond = $tensecond - 1;
      tensecond.setText($tensecond);
      }

    tensecond();

    if ( $tensecond < 0 )
    {
      $tensecond = 5;
      tensecond.setText($tensecond);

      %this.updateoneMinutes();
    } 
}

///Lowers every minute
function timerCountdown::updateoneMinutes(%this)
{
     if($scoreStop == 1)
      {
      $oneminute = $oneminute - 0;
      oneminute.setText($oneminute);
      }
     else
     if($scoreStop == 0)
      {
      $oneminute = $oneminute - 1;
      oneminute.setText($oneminute);
      }

    oneminute();

    if ( $oneminute < 0 )
    {
      $oneminute = 9;
      oneminute.setText($oneminute);
    } 
}

///Lowers from one to zero when level starts
function timerCountdown::updatetenMinutes(%this)
{
     if($scoreStop == 1)
      {
      $tenminute = $tenminute - 0;
      tenminute.setText($tenminute);
      }
     else
     if($scoreStop == 0)
      {
      $tenminute = $tenminute - 1;
      tenminute.setText($tenminute);
      }
}
#3
02/17/2009 (12:45 pm)
Hi I saw your forum post about a timer code. It is a really old post and I am quite new to TGB so I have had some problems implementing it. It can be that the code is old and that is why it won’t work or it might be that I am new and doing something wrong or it might be both witch would make it even harder for me to get it to work by myself… and also explain why it isn’t working.
It is exactly the kind of thing that I am looking for so if you would like to help me whit this I would be weary happy.