micha wrote:Username wrote:The problem when using love.keyboard.isDown is that you cannot control the repeat timing due to the different dt's that may have different computers, so in a slow computer I may press left arrow key to walk and the character will move relatively slow, while an user with a top-end computer may do the same and the character will move at light speed. The only solution to this is to limit the framerate to a fixed amount.
I would definitely use only love.keypressed plus keyrepeat.
Sorry, I don't understand how this is related to keypressed and isDown. What you talk about is just a matter of correctly using dt. Or do you mean something different?
Also the wiki says about keyrepeat:
Please note that a generally better way to move an object would be to put code in love.update() which uses love.keyboard.isDown.
The problem when using isDown plus messing with dt is that it will make the game movement to look jumpy if dt is not the same as the person that has designed the game. This does not happen with keypressed + keyrepeat
For example, lets say I do a game, and my game runs at dt = 0.01.
my character in screen moves with posX = posX + dt*100
The game in my computer makes the character to move 1 pixel each game cycle, which is 0.01 seconds for me.
Now, you may run the same game in your computer, but for some reason, your dt is 0.05, so you get posX = posX + 0.5*100
The character in your computer moves at 5 pixels per game tic, so if you need to stop movement at pixel number 3, you simply can't.
Now, using keypressed + keyrepeat, it doesn't mind the dt of the computers because the character movement amount is not modified by the time invested by the computer to complete one game cycle and the character will always move 1 pixel away while the key is pressed, so i can stop at pixel 3 if i want.
Run this piece of code and you will see what i mean.
Use left/right arrow keys to move the green line.
'd' to increase by 0.05 deltatime or retunr it to normal.
Now, with deltatime +0.05, try putting the green line over the large lines.
Next do the same but with your default deltatime that will probably be 0.01 or something.
Code: Select all
posX = 400
posY = 300
sleepTime = 0
function love.draw()
love.graphics.setColor(100, 100, 100, 100)
for i = 0, 800 do
love.graphics.line( (i - 1)*32, 0, (i - 1)*32, 600 )
end
if math.mod(math.floor(posX), 32) == 0 then
love.graphics.setColor(255, 0, 0, 255)
love.graphics.line( posX, 0, posX, 600 )
end
love.graphics.setColor(0, 255, 0, 255)
love.graphics.line( posX, 250, posX, 350 )
love.graphics.print("FPS are " .. love.timer.getFPS() .. "\ndt is " .. love.timer.getDelta() .. "\nPress 'd' to toggle deltatime", 10, 10)
end
function love.update(dt)
if love.keyboard.isDown("left") then
posX = posX - dt*100
end
if love.keyboard.isDown("right") then
posX = posX + dt*100
end
love.timer.sleep(sleepTime)
end
function love.keypressed(key, unicode)
if unicode == 100 then
if sleepTime == 0 then
sleepTime = 0.05
posX = 400
else
sleepTime = 0
posX = 400
end
end
end