Currently in my code, there is a player object that moves smoothly, my only problem is that I can't get it to move forwards directly as in I can scroll side to side with 'A' and 'D' but not directly forward with 'W' and 'S'. Button combos such as 'W+A' and 'A+S' work though...Please help out!!
function love.load()
love.graphics.setBackgroundColor(100, 149, 237)
x = 50
y = 50
speed = 100
playerIdle = love.graphics.newImage("gfx/Person/0.png")
playerLeft = love.graphics.newImage("gfx/Person/0.png")
playerRight = love.graphics.newImage("gfx/Person/0.png")
playerDown = love.graphics.newImage("gfx/Person/0.png")
playerUp= love.graphics.newImage("gfx/Person/0.png")
player = playerIdle --no movement
end
function love.update(dt)
if love.keyboard.isDown("d") then
x = x + (speed * dt)
player = playerRight
elseif love.keyboard.isDown("a") then
x = x - (speed * dt)
player = playerLeft
if love.keyboard.isDown("s") then
y = y + (speed * dt)
player = playerDown
elseif love.keyboard.isDown("w") then
y = y - (speed * dt)
player = playerUp
end
end
end
function love.draw()
love.graphics.draw(player, x, y)
love.graphics.draw(playerLeft, x, y, math.rad(45), -1, 1, width, 0)
end
You end the "elseif love.keyboard.isDown("a")" statement after the up and down movement part, instead of right after that statement. That's why left-diagonal movement works, because as your code is written it will only check for vertical movement if "a" is held down.
function love.update(dt)
if love.keyboard.isDown("d") then
x = x + (speed * dt)
player = playerRight
elseif love.keyboard.isDown("a") then
x = x - (speed * dt)
player = playerLeft
--right here :)
end
--^
if love.keyboard.isDown("s") then
y = y + (speed * dt)
player = playerDown
elseif love.keyboard.isDown("w") then
y = y - (speed * dt)
player = playerUp
end
end