【发布时间】:2013-05-30 22:54:55
【问题描述】:
我有一个旨在动态更改 3D 数组或表格大小的函数,但它一直在说它返回值 nil,因为它超出了界限。这是它的代码:
function resize()
temp = { }
for h=1, height do
table.insert( temp , { } )
for y=1, length do
table.insert ( temp[h], { } )
for x=1, width do
num = 16
if #blocks <= height then
if #blocks[h] <= length then
if #blocks[h][y] <= width then
num = blocks[h][y][x]
end
end
end
table.insert( temp[h][y] , num )
end
end
end
blocks = temp
end
我知道它的评论不是很好,但想法是它创建一个具有更改维度的新表,然后将块的数据叠加在新表上,最后用新的临时表覆盖块。
长度宽度和高度改变一个,增加或减少,但一次只能改变一个。
我不确定我是否解释得足够清楚,如果没有让我知道,我会尝试更详细地解释。
谢谢大家, 詹姆斯
【问题讨论】:
-
请添加确切的错误消息并在代码中标记该行
-
我可能遗漏了一些东西,但为什么不直接从现有表中添加或删除元素,而不是移动所有数据呢?