function love.load()
love.graphics.setBlendMode('alpha')
love.keyboard.setKeyRepeat(100, 100)
PlayerShip = love.graphics.newImage("PlayerShip.png")
PlayerShipX = 350
PlayerShipY = 100
end
function love.keypressed(k)
if k == 'escape' then
love.event.push('q') --Quits the game
end
end
function love.update()
if love.keyboard.isDown('up') then
PlayerShipY = PlayerShipY - 1
end
if love.keyboard.isDown('down') then
PlayerShipY = PlayerShipY + 1
end
if love.keyboard.isDown('left') then
PlayerShipX = PlayerShipX - 1
end
if love.keyboard.isDown('right') then
PlayerShipX = PlayerShipX + 1
end
end
function love.draw()
love.graphics.toggleFullscreen()
love.graphics.draw(PlayerShip, PlayerShipX, PlayerShipY)
end
There's really only one problem with this code... love.keyboard.isDown() doesn't seem to register keydowns, but rather it seems to register keypresses! I.E my spaceship doesn't move when I hold my key down, only when I press it continiously. Any suggestions?
I've never heard of anything like that... does that still happen when you remove the setKeyRepeat line?
(As a side note: do you know about dt? I recommend you to use it, because it will help to provide a consistent experience, independent from what computer the player uses.)
Yeah it still happens if I remove that line. I added that line because I figured it might solve the issue, but it didn't help at all. I have no idea what setKeyRepeat() actually does, either. The documentation wasn't very good.
Hm... I should probably at least add dt as an argument to the update function!
This isn't odd AT ALL, let me explain it to you.
love.keypressed is called whenever a key is pressed, once for every key, and the key repeat just calls the keypressed callback again after a while.
love.keyboard.isDown works as you want it to, but it has to be run every frame for it to work (the way you intend it to), and since it is called from within keypressed, it isn't called every frame.
To do what you want you should move the key checking code (with the isDown calls) to love.update, add a dt argument and change + 1 to + dt.