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

361 views Asked by At

How do i make a repeat look in LOVE2D so i can make a character moves?

I've tried

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

but it didn't worked.Please help me!

1

There are 1 answers

0
Thelmund On

It sounds like you're trying to move an image when a key is held down. Using a 3rd party library timer is overly complicated for this.

You want to associate some X and Y variables with the image and draw the image using those variables. You can change them by using the love.keypressed callback or by checking for key presses in love.update if you want continuous movement.

Example:

function love.load()
    sprite = {x=0, y=0, image=love.graphics.newImage("test.jpg")}
    speed = 3
end

function love.update(dt)
    if love.keyboard.isDown("left")  then sprite.x = sprite.x - speed * dt end
    if love.keyboard.isDown("right") then sprite.x = sprite.x + speed * dt end
    if love.keyboard.isDown("up")    then sprite.y = sprite.y - speed * dt end
    if love.keyboard.isDown("down")  then sprite.y = sprite.y + speed * dt end
end

function love.draw()
    love.graphics.draw(sprite.image, sprite.x, sprite.y)
end