Timer Problems (SOLVED)

Started by Creator, Mon 15/09/2008 07:52:06

Previous topic - Next topic

Creator

I'm making a timer that counts down from 4 minutes, 2 minutes and 1 minute. When the timer's second's counter gets to less then 10 I want it to display: 0:09, 0:08 etc.
My code works except that the second the timer reaches "*minutes left*:10" it jumps immediately to "*minutes left*:09" after 1 game loop instead of 40.
Heres my
Code: ags

int timeLeftMins;
int timeLeftSecs;
#sectionstart repeatedly_execute  // DO NOT EDIT OR REMOVE THIS LINE
function repeatedly_execute() 
{
   if (timeLeftSecs > 0) {
     if (timeLeftSecs > 0) {
     timeLeftSecs--;
}
     if ((timeLeftSecs == 0) && (timeLeftMins > 0)) {
       lblTimeLeft.Text = String.Format("Time left: %d:0" , timeLeftMins);
       timeLeftMins--;
       timeLeftSecs = 59;
}
     if (timeLeftSecs < 10) {
       lblTimeLeft.Text = String.Format("Time left: %d:0%d", timeLeftMins, timeLeftSecs);
  }
     if ((timeLeftMins == 0) && (timeLeftSecs == 0)) {
       Wait(40);
       lblTimeLeft.Text = "Time left: 0:00";
       Wait(1);
       PlaySound(1);
       Display("Times Up!");
       lblTimeLeft.Text = "Time left: ?";
       mouse.Mode = eModePointer;
       return;
    }
      Wait(40);
      lblTimeLeft.Text = String.Format("Time left: %d:%d", timeLeftMins, timeLeftSecs);
}
    // put anything you want to happen every game cycle here
  }
#sectionend repeatedly_execute  // DO NOT EDIT OR REMOVE THIS LINE


Can anyone help please?
(Using AGS 3.0.2 SP 1 if that helps)

Gilbert

Instead of checking whether the "seconds" count has become a 1-digit number you can always use the %02d token in a string to pad the value with '0' when it is less than 2 digits.

There are also some repeated codes. I'll try to fix them and reformat the codes in a simpler form (sorry, I was too lazy to read the original codes line by line to find the real problem), see if that fixes your problems.
Code: ags

int timeLeftMins;
int timeLeftSecs;
#sectionstart repeatedly_execute  // DO NOT EDIT OR REMOVE THIS LINE
function repeatedly_execute() 
{
  if (timeLeftSecs >= 0) timeLeftSecs--;
  else if (timeLeftMins > 0) {
       timeLeftMins--;
       timeLeftSecs = 59;
  } else {
       Wait(40);
       lblTimeLeft.Text = "Time left: 0:00";
       Wait(1);
       PlaySound(1);
       Display("Times Up!");
       lblTimeLeft.Text = "Time left: ?";
       mouse.Mode = eModePointer;
       return;
  }
  Wait(40);
  lblTimeLeft.Text = String.Format("Time left: %d:%02d", timeLeftMins, timeLeftSecs);
}
#sectionend repeatedly_execute  // DO NOT EDIT OR REMOVE THIS LINE


Creator

#2
I had to make a few changes to your code Gilbot to get it to work correctly, but the changes you made where more than enough yo get the extra seconds digit to appear.
This is my code now:
Code: ags

int timeLeftMins;
int timeLeftSecs = -1;
#sectionstart repeatedly_execute  // DO NOT EDIT OR REMOVE THIS LINE
function repeatedly_execute() 
{
  if (timeLeftSecs >= 0) { 
    timeLeftSecs--;
   if ((timeLeftSecs < 0) && (timeLeftMins > 0)) {
// Seconds must be less than zero or it jumps straight to *minutes left*:59 from *minutes left+1*:01
       timeLeftMins--;
       timeLeftSecs = 59;
  } 
  else if ((timeLeftSecs == 0) && (timeLeftMins == 0)) {
       Wait(40);
       lblTimeLeft.Text = "Time left: 0:00";
       Wait(1);
       PlaySound(1);
       Display("Times Up!");
       lblTimeLeft.Text = "Time left: ?";
       mouse.Mode = eModePointer;
       return;
  }
  Wait(40);
  lblTimeLeft.Text = String.Format("Time left: %d:%02d", timeLeftMins, timeLeftSecs);
}
}
#sectionend repeatedly_execute  // DO NOT EDIT OR REMOVE THIS LINE

Thanks a lot for your help.
Also can I ask how or why the '%02d' text puts that extra 0 there?

Gilbert

This is just the format for specifying this kind of displays. Read this.

I'm not familiar with C style programming, maybe it's just how people do with standard C.

SMF spam blocked by CleanTalk