function love.load()
player = {}
player.x = 400
player.y = 300
player.speed = 5
player.size = 10
end
function love.update(dt)
if love.keyboard.isDown("d") then
player.x = player.x + player.speed
end
if love.keyboard.isDown("a") then
player.x = player.x - player.speed
end
if love.keyboard.isDown("s") then
player.y = player.y + player.speed
end
if love.keyboard.isDown("w") then
player.y = player.y - player.speed
end
end
function love.draw()
love.graphics.circle("fill", player.x, player.y, player.size)
end
You simply have to treat the conditions as mutually exclusive, meaning that only one of the movement directions can be active at any one time. The simplest way to achieve this is changing the if-statements into an if-elseif-chain, like the following:
function love.update(dt)
if love.keyboard.isDown("d") then
player.x = player.x + player.speed
elseif love.keyboard.isDown("a") then
player.x = player.x - player.speed
elseif love.keyboard.isDown("s") then
player.y = player.y + player.speed
elseif love.keyboard.isDown("w") then
player.y = player.y - player.speed
end
end
This does, however, give the code a preference on how directions are handled. Like if you were to hold all the buttons down, the first one ("d") would be picked over the rest. This may not be too bothersome in a slow-moving RPG like Pokémon Red & Blue, but if you were to increase the movement speed you might want to do some additional checks (eq. holding "a" and "d" cancel each other out, resulting in no movement, etc).
MrFariator wrote: ↑Tue Feb 08, 2022 10:41 pm
You simply have to treat the conditions as mutually exclusive, meaning that only one of the movement directions can be active at any one time. The simplest way to achieve this is changing the if-statements into an if-elseif-chain, like the following:
function love.update(dt)
if love.keyboard.isDown("d") then
player.x = player.x + player.speed
elseif love.keyboard.isDown("a") then
player.x = player.x - player.speed
elseif love.keyboard.isDown("s") then
player.y = player.y + player.speed
elseif love.keyboard.isDown("w") then
player.y = player.y - player.speed
end
end
This does, however, give the code a preference on how directions are handled. Like if you were to hold all the buttons down, the first one ("d") would be picked over the rest. This may not be too bothersome in a slow-moving RPG like Pokémon Red & Blue, but if you were to increase the movement speed you might want to do some additional checks (eq. holding "a" and "d" cancel each other out, resulting in no movement, etc).
99 wrote: ↑Tue Feb 08, 2022 9:51 pmBRAND new to using Love2d (Literally started yesterday)
Just make it work like Pokemon Red & Blue
This is my current Code (Please don't make fun of me, I started yesterday)
That's actually really good for having just started yesterday! Here's a quick tip on another way to initialize a table. It initializes the player table in 1 line instead of several. (Totally optional to use it, of course - do what makes sense to you!)