How do I use hump.timer in a repeat loop in LOVE2D?

2

I'm asking this to make something move in LOVE2D when a key is pressed.

I've already tried

repeat
    imgx = imgx + 1
    timer.after(0, function() end)
until not love.keyboard.isDown('left')

But it did not work. Please help me!

Entire code:

function love.load()
    timer = require 'hump.timer'
    face = love.graphics.newImage("face.png")
    imgx = 0
    imgy = 0
end

function love.keypressed(key)
    if key == 'left' then
        repeat
            imgx = imgx + 1
            timer.after(0, function() end)
        until not love.keyboard.isDown('left')
    end
end

function love.update(dt)
    timer.update(dt)
end

function love.draw()
    love.graphics.draw(face, imgx, imgy)
end
    
asked by anonymous 01.01.2017 / 01:41

1 answer

0

I recommend not using a native loop to change the x-coordinate of the image. I call it synchronous work. While this loop executes, the program will be stopped (however the imgx will still change).

(If you eventually get used to running timer#after on loops with no yields to do something will end up creating problems.)

I do not think the problem has anything to do with it anyway.

Try using Timer#every (ps: I do not know if this solves the problem , because I do not know LOVE2D well):

function love.load()
    timer = require 'hump.timer'
    face = love.graphics.newImage 'face.png'
    imgx = 0
    imgy = 0
end

local function tick()
    imgx = imgx + 1;
    return love.keyboard.isDown 'left';
end

function love.keypressed(key)
    if key == 'left' then
        timer.every(.1, tick); -- atraso: 100 milisegundos
    end
end

function love.update(dt)
    timer.update(dt)
end

function love.draw()
    love.graphics.draw(face,imgx,imgy)
end
    
01.01.2017 / 12:30