lua - 表维护(粒子系统相关)
下面的 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。
The update()
function below gets called on every frame of a game. If the Drop
particle has y value greater than 160 I want to remove it from the table. The problem is that I get "attempt to compare number with nil" errors, on the line notated below:
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
What am I doing wrong? Obviously val
is nil, but I don't understand why the table iteration would find val in the first place since I set it to nil when it's y value gets larger than 160.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
感谢您的回答,他们都很有帮助。这就是最终对我有用的东西。
table.remove
调用对于保持循环正常运行是必要的。Thanks for the answers, they were all helpful. Here is what ended up working for me. The
table.remove
call is necessary to keep the loop running properly.您找错地方了,问题不在于
val
是nil
,而是val.y
是nil< /代码>。请参阅此示例:
另外,当您将
val
设置为nil
时,可能不会执行任何操作(我相信val
是一个副本):编辑:要从表中删除元素,您需要
table.remove
:You're looking in the wrong place, the problem isn't that
val
isnil
, it'sval.y
that'snil
. See this example:Also, when you set
val
tonil
, that may not be doing anything (I believeval
is a copy):Edit: to delete an element from a table, you want
table.remove
:JeffK 的解决方案应该有效,但我认为它有效的原因不是因为他向后遍历列表,而是因为他设置的是
articles[i] = nil
而不是val =零
。如果运行val = nil
,您只是将 val 的本地副本设置为 nil,而不是表中的条目。试试这个:
JeffK's solution should work, but I think the reason it will work is not because of the fact that he's traversing the list backwards, but because he is setting
particles[i] = nil
instead ofval = nil
. If you runval = nil
you're only setting the local copy of val to nil, not the entry in the table.Try this:
我认为当 ipairs 迭代表时,您不能修改表的内容。我依稀记得读过我的Lua 5.1参考手册的硬拷贝,但我现在似乎找不到它。当您将 val 设置为 nil 时,它会从粒子中删除一个元素 em> 表。
您可以尝试反向处理该表,因为您的函数正在对粒子表进行全面扫描,有条件地删除一些项目:
I don't think you are allowed to modify the contents of a table while ipairs is iterating through it. I vaguely remember reading that my hardcopy of the Lua 5.1 Reference Manual, but I can't seem to locate it now. When you set val to nil, it removes an element from the particles table.
You might try processing the table in reverse, since your function is doing a full sweep of the particles table, conditionally removing some items: