首页 > 解决方案 > Lua:“:40:'}'预计(在'='附近关闭'{'在第37行)”

问题描述

错误提示我缺少 },但我正在关闭 {}。我正在使用 Lua 和 TIC-80 编写 2D 游戏。我一直在尝试解决这个问题,但我不知道这里有什么问题。谁能帮帮我吗?该错误表明问题出在“inferiorRight”对象中,并且我没有关闭“{”

function tryToMoveTo(myDisplacementY)
    superiorLeft = {
        x = player.x - 8,
        y = player.y - 8 - 1
    }
    superiorRight = {
        x = player.x + 4,
        y = player.y - 8 - 1
    }
    inferiorRight = {
        x = player.x + 7,
        y = player.y + 7,
        player.y = player.y + 7 + myDisplacementY
    }
    inferiorLeft = {
        x = player.x - 8,
        y = player.y + 7 + myDisplacementY
    }
        
    if collidesWithMap(inferiorRight) or
     collidesWithMap(inferiorLeft) or
        collidesWithMap(superiorRight) or
        collidesWithMap(superiorRight) then
     -- collides
    else
        player.y = player.y + myDisplacementY
    end
end

function update()
    -- up
    if btn(0) then
      tryToMoveTo(-1)
    end
    -- down
    if btn(1) then
      tryToMoveTo(1)                
    end
    -- left
    if btn(2) then
      player.x = player.x - 1
    end
    -- right
    if btn(3) then
      player.x = player.x + 1
    end
end

标签: lua

解决方案


这不是player.y用作表键的正确语法:

inferiorRight = {
    x = player.x + 7,
    y = player.y + 7,
    player.y = player.y + 7 + myDisplacementY --<--
}

如果您确实想拥有player.y作为密钥,则需要将其写为["player.y"].


推荐阅读