Timer/sleep on love2d

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
fireplace2015
Prole
Posts: 2
Joined: Sat Feb 07, 2015 8:42 pm

Timer/sleep on love2d

Post 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")
davisdude
Party member
Posts: 1154
Joined: Sun Apr 28, 2013 3:29 am
Location: North Carolina

Re: Timer/sleep on love2d

Post 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.
GitHub | MLib - Math and shape intersections library | Walt - Animation library | Brady - Camera library with parallax scrolling | Vim-love-docs - Help files and syntax coloring for Vim
fireplace2015
Prole
Posts: 2
Joined: Sat Feb 07, 2015 8:42 pm

Re: Timer/sleep on love2d

Post 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
davisdude
Party member
Posts: 1154
Joined: Sun Apr 28, 2013 3:29 am
Location: North Carolina

Re: Timer/sleep on love2d

Post 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
GitHub | MLib - Math and shape intersections library | Walt - Animation library | Brady - Camera library with parallax scrolling | Vim-love-docs - Help files and syntax coloring for Vim
Post Reply

Who is online

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