Timer to end game

Questions about the LÖVE API, installing LÖVE and other support related questions go here.
Forum rules
Before you make a thread asking for help, read this.
Post Reply
DKipppp
Prole
Posts: 1
Joined: Thu Jan 23, 2020 5:35 am

Timer to end game

Post by DKipppp »

How do I make an easy timer to end a game after like 2 seconds. I made a small asteroids game and once you win you can shoot into the air. I just want to make a timer so you can only shoot in the air for 2 seconds and then the window closes
User avatar
pgimeno
Party member
Posts: 3548
Joined: Sun Oct 18, 2015 2:58 pm

Re: Timer to end game

Post by pgimeno »

You can use a timer library or make your own. If you decide to use one, hump.timer is good. It can be overkill for just one timer, though, so if you decide to make your own, it's easy. Have a variable that tells if the timer is active. If it is, accumulate dt in another variable until the value exceeds the set timer. For example:

Code: Select all

local timerActive = false
local timerDuration = 0

function love.update(dt)
  if timerActive then
    timerDuration = timerDuration + dt
    -- after 2 seconds, quit
    if timerDuration >= 2 then
      love.event.quit()
    end
  end

  ... rest of love.update here

end
You then set timerActive = true when the player wins, so that the timer starts counting.
Post Reply

Who is online

Users browsing this forum: No registered users and 76 guests