Remove Diagonal Movement (Like in Classic Pokemon Games)

Questions about the LÖVE API, installing LÖVE and other support related questions go here.
Forum rules
Before you make a thread asking for help, read this.
Post Reply
99
Prole
Posts: 3
Joined: Tue Feb 08, 2022 9:41 pm

Remove Diagonal Movement (Like in Classic Pokemon Games)

Post by 99 »

BRAND 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)

Code: Select all

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
Thanks! :awesome:
MrFariator
Party member
Posts: 577
Joined: Wed Oct 05, 2016 11:53 am

Re: Remove Diagonal Movement (Like in Classic Pokemon Games)

Post by MrFariator »

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:

Code: Select all

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
Prole
Posts: 3
Joined: Tue Feb 08, 2022 9:41 pm

Re: Remove Diagonal Movement (Like in Classic Pokemon Games)

Post by 99 »

Thank you so much! :awesome:




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:

Code: Select all

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).
User avatar
milon
Party member
Posts: 472
Joined: Thu Jan 18, 2018 9:14 pm

Re: Remove Diagonal Movement (Like in Classic Pokemon Games)

Post by milon »

99 wrote: Tue Feb 08, 2022 9:51 pm BRAND 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!)

Code: Select all

function love.load()
    player = {x=400, y=300, speed=5, size=10}
end
Any code samples/ideas by me should be considered Public Domain (no attribution needed) license unless otherwise stated.
Post Reply

Who is online

Users browsing this forum: Bing [Bot], Google [Bot], Semrush [Bot] and 3 guests