4

所以我对 Lua 还很陌生,在其他语言中,我已经能够创建一个 2D 变量数组并简单地通过数组索引以创建一个平铺地图。每当我在 lua 中尝试这个时,我都会收到一个错误(特别是一个错误,指出我正在索引一个 nil 值)。我怎样才能解决这个问题?

代码

function love.load()
love.graphics.setColor(255,255,0)
tile = love.graphics.newImage("lightGrass.png")
map = { {1,1,0,0,0,0,0,0,0,0},
        {0,1,0,0,0,0,0,0,0,0},
        {0,1,0,0,0,0,0,0,0,0},
        {1,1,0,0,0,0,0,0,0,0},
        {0,0,0,0,0,0,0,0,0,0},
        {0,0,0,0,0,0,0,0,0,0},
        {0,0,0,0,0,0,0,0,0,0},
        {0,0,0,0,0,0,0,0,0,0},
        {0,0,0,0,0,0,0,0,0,0},
        {0,0,0,0,0,0,0,0,0,0}
    }
end

function love.draw()
    for i = 0, 10 do
        for j = 0, 10 do
            newPos = map[i][j]
            if newPos == 0 then -- this is where the error is!!!!!!!!!!!!!!!
                love.graphics.draw(tile,j * 32, i * 32)
            end
        end
    end

end

function love.update(dt)

end
4

1 回答 1

4

Lua 中的数组从 1 开始,而不是从 0。所以你的for循环必须从 1 开始。

于 2014-02-25T04:09:13.043 回答