Page 1 of 1
Timer/sleep on love2d
Posted: Sat Feb 07, 2015 8:51 pm
by fireplace2015
Trying to make a game but the game freeze when using "love.timer.sleep".
Code: Select all
print("light on")
--need small code to make it stay "light on" for example 5sec.
os.execute("clear")
print("light off")
Re: Timer/sleep on love2d
Posted: Sat Feb 07, 2015 9:21 pm
by davisdude
You probably shouldn't use love.timer.sleep inside of love.update for that long- that's why it freezes.
If you want the screen to be a light color for 5 seconds (if that's what you mean) do something like this:
Code: Select all
local function startTimer()
time = 0
countTime = true
color = { 255, 255, 255 }
end
local function endTimer()
countTime = false
color = { 0, 0, 0 }
end
function love.load()
color = { 0, 0, 0 } -- The color to set the screen to.
countTime = false
time = 0
maxTime = 5
end
function love.update( dt )
if countTime then
time = time + dt
if time >= maxTime then
endTimer()
end
end
end
function love.draw()
love.graphics.setColor( color )
love.graphics.rectangle( 'fill', 0, 0, 800, 600 )
end
function love.keyreleased( key )
if not countTime then startTimer() end
end
In this example, release any key to set the screen to white for 5 seconds.
Re: Timer/sleep on love2d
Posted: Sat Feb 07, 2015 9:45 pm
by fireplace2015
Better example of the problem
Code: Select all
function love.load()
light_on = love.graphics.newImage("light_on.png")
light_off = love.graphics.newImage("light_off.png")
set = light_off
sleep = 0
switch = true
end
function love.update(dt)
if switch then
while sleep < 5 do
sleep = sleep + dt
set = light_on
end
switch = false
set = light_off
end
end
function love.draw()
love.graphics.draw(set,0,0)
end
Re: Timer/sleep on love2d
Posted: Sat Feb 07, 2015 11:19 pm
by davisdude
Same basic principle. You shouldn't use while-loops in love.update, you should use dt: the time between frames. You could also take a look at some clock libs, like
cron.lua or
tick