所以我对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
答案 0 :(得分:4)
Lua中的数组从1开始,而不是从0开始。因此,for
循环必须从1开始。