无法使用(尝试索引字段'?'(为 nil 值))对 2D 数组进行索引。

我在Lua中设置了2个不同的2D数组。第一个循环:

bubbleMat = {}                              --设置名为bubbleMat的表
for i = 1, 10 do
    bubbleMat[i] = {}                           --1D表,有10个组件

    for j = 1, 13 do
        bubbleMat[i][j] = bubbleClass.new( (i*62) - 62, (j*62) - 62 )   --一个有10x13矩阵的2D表,每个单元都会在循环中给定一个坐标
    end
end

通过以下方式,我可以将数组中任何位置的值打印到控制台:

print(bubbleMat[x][y])

对于任意x和y的数字。

出于某种原因,第二个数组不起作用。第二个数组如下:

bubbleMat = {}                             --设置名为bubbleMat的表
for j = 1, 13 do
    for i = 1, 10 do
        bubbleMat[i] = {}
        --bubbleMat[i][j] = {}
            if j%2 == 0 then
                bubbleMat[i][j] = bubbleClass.new( (i*62) - 31, (j*62) - 62 )
            else
                bubbleMat[i][j] = bubbleClass.new( (i*62) - 62, (j*62) - 62 )
            end
    end
end

print(bubbleMat)

我不确定为什么无法索引第二个数组。

这是我在控制台中收到的错误:

尝试索引字段“?”(空值)

提前感谢任何帮助。

基本上,我想显示一个以以下模式存储在2D数组中的气泡网格

0   0    0    0    0    0    0    0    0    0
  0    0    0    0    0    0    0    0    0

而不是在下一行中将气泡直接放置在下面。

点赞
用户1008957
用户1008957

循环for j在外面,循环for i在里面。这不一定是错误的,但是不寻常。

然而,在最内层循环中,bubbleMat[i]被初始化为{},这显然是错误的。

要么将该初始化转移到最外层循环中,要么使用这种语法:

bubbleMat[i] = bubbleMat[i] or {}
2012-09-09 17:30:03