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

Asked

Viewed 95 times

3

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 didn’t 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

1 answer

0

I recommend not using a native loop to change the x-string of the image. I call it synchronous work. While this loop runs, the program will be stopped (but the imgx will yet change).

(If you ever get used to performing timer#after in loops without yields to do something will end up creating problems.)

I don’t think the problem has anything to do with it, though.

Try using Timer#every (p.s.: I don’t know if this solves the problem, because I’m not familiar with LOVE2D):

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
  • It didn’t work... I tried to use love.graphics.print(imgx,400,300),but the value was 0 and did not change

  • @arthurgps2 Hm, I’ll have to learn LOVE2D then

Browser other questions tagged

You are not signed in. Login or sign up in order to post.