【问题标题】:Love2D Tile Based Collision Not WorkingLove2D 基于瓷砖的碰撞不起作用
【发布时间】:2014-04-04 01:28:50
【问题描述】:

我在瓷砖碰撞方法中遇到问题。出于某种原因,玩家可以通过一些不应通过的图块。另外,我不完全确定为什么,但是当它卡住时,它可以通过物体向左移动,但只能向左移动。我在下面发布了一些代码,如果有人能指出我正确的方向,那就太好了。 (如果有人能找到一个快速的解决方案,那就更好了!)我的玩家移动方法和瓷砖碰撞方法都在更新方法中调用。

地图和瓷砖碰撞

    map = { {1,1,1,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3},
        {1,1,1,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3},
        {1,1,1,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3},
        {1,1,1,1,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3},
        {1,1,1,1,1,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3},
        {1,1,1,1,1,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3},
        {2,1,1,1,1,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3},
        {2,2,1,1,1,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3},
        {2,2,1,1,1,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3},
        {2,2,1,1,1,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3},
        {2,2,1,1,1,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3},
        {2,2,1,1,1,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3},
        {2,2,1,1,1,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3},
        {2,2,1,1,1,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3},
        {2,2,1,1,1,3,3,1,1,1,1,1,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3},
        {2,2,1,1,1,3,3,1,1,1,1,1,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3},
        {2,2,1,1,1,3,3,1,1,1,1,1,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3},
        {1,1,1,1,1,1,1,1,1,1,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3},
        {1,1,1,1,1,1,1,1,1,1,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3},
        {1,1,1,1,1,1,1,1,1,1,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3},
        }

function testTile(x,y)
    if map[y][x + 1] == 1 then
        canRight = true
    end

    if map[y][x + 1] ~= 1 then
        canRight = false
    end

    if map[y][x - 1] == 1 then
        canLeft = true
    end

    if map[y][x - 1] ~= 1 then
        canRight = false
    end

    if map[y + 1][x] == 1 then
        canDown = true
    end

    if map[y + 1][x] ~= 1 then
        canDown = false
    end

    if map[y - 1][x] == 1 then
        canUp = true
    end

    if map[y - 1][x] ~= 1 then
        canUp = false
    end
end

function movePlayer(dt)

    if love.keyboard.isDown("right") and canRight then
        playerX = playerX + 1 * dt
    end

    if love.keyboard.isDown("left") and canLeft then
        playerX = playerX - 1 * dt
    end

    if love.keyboard.isDown("down") and canDown then
        playerY = playerY + 1 * dt
    end

    if love.keyboard.isDown("up") and canUp then
        playerY = playerY - 1 * dt
    end
end

【问题讨论】:

  • 欢迎来到 StackOverflow。为问题分配标签时请小心谨慎,并在 this 网站上查看其含义的描述。例如,由于这是一个面向专业程序员和其他程序员的网站,因此 RPG 标签指的是主要由专业程序员使用的 RPG 语言,而不是游戏玩家的松散术语。

标签: lua collision-detection tile love2d


【解决方案1】:

当它卡住时留下的原因可能是因为您在testTile(x,y) 的第四个if 语句中有错字。

你写的

if map[y][x - 1] ~= 1 then
    canRight = false
end

应该是的

if map[y][x - 1] ~= 1 then
    canLeft = false
end

【讨论】:

    【解决方案2】:

    原因是打字错误:

    if map[y][x - 1] ~= 1 then
        canRight = false;
    end
    

    如果您无法移动 left,请将 canRight 设置为 false。这可能是复制/粘贴错误,所以 如果您复制/粘贴代码,请务必小心。这是最常见的错误之一,而且这种错误很难被发现:P

    另一个小建议:不要使用

    if map[y][x - 1] == 1 then
        canLeft = true;
    end
    if map[y][x - 1] ~= 1 then
        canLeft = false;
    end
    

    你可以使用

    else canLeft = false;
    

    这会使代码更短,在我看来也更简洁。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-01-12
      • 2014-05-09
      • 1970-01-01
      • 2012-04-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-01-26
      相关资源
      最近更新 更多