Love2d / LUA网格锁定运动没有对角线

问题描述

我认为这将是一个普遍的问题,但是经过几天的研究,我找不到解决方案。对于一般的编程和LUA而言,这是非常新的。我正在将一个SUPAPLEX克隆构建为CS50个人项目:角色沿着基于网格的地图移动,并且似乎每个人都建议(附上)一个代码。松开箭头按钮,将继续平稳地移动直到瓷砖的末端。但是,如果按下2个移动按钮,会导致短暂的对角线移动,这就是我未能成功解决的问题。

基本上,我试图忽略任何输入,直到精灵的移动在网格图块的末尾完成为止,或者阻止更新直到在一个方向上的移动完成为止。看起来很简单,但是我将放弃整个事情。令人沮丧非常感谢任何输入,我相信这会对很多人有很大帮助...

function love.load()

    love.keyboard.setKeyRepeat(true)

    player = {
        grid_x = 256,grid_y = 256,act_x = 256,act_y = 256,speed = 5,}

    map = {
        { 1,1,1 },{ 1,1 }
    }

    
    function testMap(x,y)
        if map[(player.grid_y / 32) + y][(player.grid_x / 32) + x] == 1 then
            return false
        end
        return true
    end


    function love.keypressed(key)
        if key == "up" then
            player.grid_y = player.grid_y - 32
        elseif key == "down" then
            player.grid_y = player.grid_y + 32
        elseif key == "left" then
            player.grid_x = player.grid_x - 32
        elseif key == "right" then
            player.grid_x = player.grid_x + 32
        end
    end

end
 
function love.update(dt)
    player.act_y = player.act_y - ((player.act_y - player.grid_y) * player.speed * dt)
    player.act_x = player.act_x - ((player.act_x - player.grid_x) * player.speed * dt)
end
 
function love.draw()
    love.graphics.rectangle("fill",player.act_x,player.act_y,32,32)
    for y=1,#map do
        for x=1,#map[y] do
            if map[y][x] == 1 then
                love.graphics.rectangle("line",x * 32,y * 32,32)
            end
        end
    end
end

解决方法

您试图让它仅沿着网格线行走吗?

取出love.keyboard.setKeyRepeat(true)
并且不要使用love.keypressed(key)

一次只能按下一次,很难使用
love.keyreleased()一起查看是否释放了所有其他键。

改用isDown,如果其中之一为Down,则其他目录键均不允许输入。 (以及您已经在更新中的夫妇player.act线)

player = {
    grid_x = 256,grid_y = 256,act_x = 256,act_y = 256,speed = 5,dir = ''
}


function love.update(dt)
    if love.keyboard.isDown("up","down","left","right") then
        if love.keyboard.isDown("up") and ( player.dir == 'up' or player.dir == '' ) then
            player.dir = 'up'  --  only go up if currently held,or no other dir key being pressed
            player.grid_y = player.grid_y - 32
        elseif love.keyboard.isDown("down") and ( player.dir == 'down' or player.dir == '' ) then
            player.dir = 'down'  --  only go down if currently held...
            player.grid_y = player.grid_y + 32
        elseif key == "left" and ( player.dir == 'left' or player.dir == '' ) then
            player.dir = 'left'
            player.grid_x = player.grid_x - 32
        elseif key == "right" and ( player.dir == 'right' or player.dir == '' ) then
            player.dir = 'right'
            player.grid_x = player.grid_x + 32
        end
    else  --  none of those keys are being pressed,so player is idle
        player.dir = ''
    end  --  isDown()

    player.act_y = player.act_y - ((player.act_y - player.grid_y) * player.speed * dt)
    player.act_x = player.act_x - ((player.act_x - player.grid_x) * player.speed * dt)
end  --  update()