我试图通过连接场地的相对边缘来创建一个无限的游戏场地。我收到以下错误:
错误:试图索引字段“?” (零值)
错误以粗体显示。据我了解,在函数 drawField() 中调用数组字段时不包含任何值,尽管它在函数 clearField()中填充了零。如何修复数组,使其值保持在clearField()之外?
local black = 0x000000
local white = 0xFFFFFF
local field = {}
local function clearField()
for gx=1,displayWidth do
if gx==displayWidth then
field[1] = field[displayWidth+1]
end
field[gx] = {}
for gy=1,displayHeight-3 do
if gy==displayHeight-3 then
field[gx][1] = field[gx][displayHeight-2]
end
field[gx][gy] = 0
end
end
end
--Field redraw
local function drawField()
for x=1, #field do
for y=1,x do
**if field[x][y]==1 then**
display.setBackground(white)
display.setForeground(black)
else
display.setBackground(black)
display.setForeground(white)
end
display.fill(x, y, 1, 1, " ")
end
end
end
-- Program Loop
clearField()
while true do
local lastEvent = {event.pullFiltered(filter)}
if lastEvent[1] == "touch" and lastEvent[5] == 0 then
--State invertion
if field[lastEvent[3]][lastEvent[4]]==1 then
field[lastEvent[3]][lastEvent[4]] = 0
else
field[lastEvent[3]][lastEvent[4]] = 1
end
drawField()
end
end
显示和事件变量是库。该程序以displayWidth = 160 和displayHeight = 50运行