i am just learning how to do coding in love2d and lua, but when i try to make this super simple up/down/left/right movement it goes diagonally, for some reason... here's the problem code(in player.lua):
player = {}
player.x = 300
player.y = 300
player.speed = 10
player.health = 20
player.damage = 2
player.pic = love.graphics.newImage("anka.png")
function playerDraw()
love.graphics.setColor(255, 255, 255)
love.graphics.draw(player.pic, player.x, player.x)
end
function playerMove()
if love.keyboard.isDown("up") then
player.y = player.y - player.speed
end
if love.keyboard.isDown("down") then
player.y = player.y + player.speed
end
if love.keyboard.isDown("left") then
player.x = player.x - player.speed
end
if love.keyboard.isDown("right") then
player.x = player.x + player.speed
end
end
to clarify myself, i did put playerMove() and playerDraw() in the love.update() and love.draw() functions in main.lua.
That looks like it should work. The main problem I see here is the fact that you aren't taking into account the timedelta between frames. What this means is that your player will move faster or slower depending on how many frames per second you get. To fix this use the dt (delta time) value of the love.update(dt) function. Once you got dt into your playerMove() (which will now look like playerMove(dt)), you can multiply the speed by the dt to fix this.