Show countdown timer upon key press?

T

traparvvaves

Guest
Hello, I am relatively new to programming in GameMaker. I am currently working on a part where if you press a key, a countdown timer will appear on the screen. However, I haven't had much luck in having it update. When I press the A key, it does show that there is 5 seconds on the clock, but it doesn't draw the countdown. This is the code I have for it:

CREATE EVENT:
Code:
global.battery = 1;
countdown_time = 5.0;
rounded_time = 0;
STEP EVENT:
Code:
if keyboard_check_released(ord('A')) {
    if global.battery > 0 {
        if (countdown_time > 0) {
            time = delta_time/1000000;
            countdown_time -= time;
            rounded_time = round(countdown_time);
        }
        else {
            countdown_time = 0;
            show_message(countdown_time);
            }
        }
    else {
        show_message("No batteries collected.");
        }
}
DRAW GUI EVENT:
Code:
draw_text(32,32, rounded_time);
Can anyone help me with this? Thank you!
 

TheouAegis

Member
you are only updating the rounded time upon key release. So it's going to keep showing the same number until you press the key again.
 
T

traparvvaves

Guest
you are only updating the rounded time upon key release. So it's going to keep showing the same number until you press the key again.
Thank you for letting me know. I tried to get rid of rounded_time and now I found out that the timer will only run if I keep pressing the key down, but will stop when I release the key. I want to be able to activate the timer to countdown all the way once I press the key once. How will I be able to do that?
 

TheouAegis

Member
well, the easiest way would be to set a variable to either true or false depending on if the timer needs to count down. When you press the key, set the variable to true. Check if the variable is true and if it is count the timer down. When the timer hits zero, set the variable to false.
 
K

Kuro

Guest
To your create event add
start_clock = false;

Then change step to something like this:
Code:
if keyboard_check_released(ord("A"))
{
   start_clock = true;
}

if (start_clock)
{

    if global.battery > 0
    {
        if (countdown_time > 0)
        {
            time = delta_time/1000000;
            countdown_time -= time;
            rounded_time = round(countdown_time);
        }
        else
        {
            countdown_time = 0;
            show_message(countdown_time);
        }
      }
      else
      {
            show_message("No batteries collected.");
       }
        
}
 
T

traparvvaves

Guest
To your create event add
start_clock = false;

Then change step to something like this:
Code:
if keyboard_check_released(ord("A"))
{
   start_clock = true;
}

if (start_clock)
{

    if global.battery > 0
    {
        if (countdown_time > 0)
        {
            time = delta_time/1000000;
            countdown_time -= time;
            rounded_time = round(countdown_time);
        }
        else
        {
            countdown_time = 0;
            show_message(countdown_time);
        }
      }
      else
      {
            show_message("No batteries collected.");
       }
       
}

Thank you so much, it worked! I had been wracking my mind for the past few hours on this, I didn't realize it was so simple! Thank you both!
 
Top