how do i make a countdown timer?

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
VXDev
Prole
Posts: 6
Joined: Sun May 28, 2023 6:12 pm

how do i make a countdown timer?

Post by VXDev »

im trying to make a timer to wait for my objects to spawn

plz help

heres my code:

Code: Select all

_G.love = require("love")



function love.load()
    listofRectangles = {}
    _G.game = {
        countdowntimer = 0
    }
    gamechart()
end

function love.update(dt)
        
    for i,v in ipairs(listofRectangles) do
        v.y = v.y + v.speed * dt
    end
end

function love.draw()
    for i,v in ipairs(listofRectangles) do
        love.graphics.rectangle("line", v.x, v.y, v.width, v.height)  
    end
end

function createRectangle()
    rectangle = {}
    rectangle.width =  50
    rectangle.height=  50
    rectangle.x     =  300
    rectangle.y     =  200
    rectangle.speed =  150
    return rectangle
end

function gamechart()
	-- heres where im trying to make it wait to spawn 
    table.insert(listofRectangles, createRectangle())
end
User avatar
dusoft
Party member
Posts: 635
Joined: Fri Nov 08, 2013 12:07 am
Location: Europe usually
Contact:

Re: how do i make a countdown timer?

Post by dusoft »

If you want to use a library: https://github.com/airstruck/knife/blob ... e/timer.md

Otherwise it's simple, use delta time:
https://love2d.org/wiki/dt
User avatar
togFox
Party member
Posts: 828
Joined: Sat Jan 30, 2021 9:46 am
Location: Brisbane, Oztralia

Re: how do i make a countdown timer?

Post by togFox »

Code: Select all


mytimer = mytimer + dt
if mytimer > 5 then
   -- do something
   mytimer = 0
end
Last project:
https://togfox.itch.io/hwarang
A card game that brings sword fighting to life.
Current project:
Turn-based PBEM horse stable (racing) management sim: https://togfox.itch.io/horse-stable-manager
https://discord.gg/HeHgwE5nsZ
User avatar
darkfrei
Party member
Posts: 1197
Joined: Sat Feb 08, 2020 11:09 pm

Re: how do i make a countdown timer?

Post by darkfrei »

Code: Select all

local function newTimer(seconds, callback)
	return {
		duration = seconds,
		callback = callback,
		elapsedTime = 0,
		active = true
	}
end

local function updateTimer(timer, dt)
	if timer.active then
		timer.elapsedTime = timer.elapsedTime + dt
		if timer.elapsedTime >= timer.duration then
			timer.active = false
			timer.callback()
			return true -- true means: timer must be deleted
		end
	end
end
:awesome: in Lua we Löve
:awesome: Platformer Guide
:awesome: freebies
Post Reply

Who is online

Users browsing this forum: Ahrefs [Bot], Bing [Bot], Google [Bot] and 3 guests