下面的update()
函数在游戏的每一帧上都会被调用。如果Drop
粒子的 y 值大于 160,我想将其从表中删除。问题是我收到“尝试将数字与零进行比较”错误,如下所示:
local particles = {};
function update()
local num = math.random(1,10);
if(num < 4) then
local drop = Drop.new()
table.insert ( particles, drop );
end
for i,val in ipairs(particles) do
if(val.y > 160) then --ERROR attempt to compare number with nil
val:removeSelf(); --removeSelf() is Corona function that removes the display object from the screen
val = nil;
end
end
end
我究竟做错了什么?显然val
是 nil,但我不明白为什么表迭代会首先找到 val,因为当它的 y 值大于 160 时我将它设置为 nil。