English 中文(简体)
lua表维护(粒子系统相关)
原标题:lua - table maintenance (particle system related)

下面的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

我做错了什么?显然,<code>val</code>是nil,但我不明白为什么表迭代会首先找到val,因为当它的y值大于160时,我将其设置为nil。

最佳回答

谢谢你的回答,他们都很有帮助。以下是最终对我有效的方法。<code>table.remove</code>调用是保持循环正常运行所必需的。

for i = #particles, 1, -1 do
    if particles[i].y > 160 then
        local child = table.remove(particles, i)
        if child ~= nil then
            display.remove(child)
            child = nil
        end
    end
end
问题回答

你找错地方了,问题不在于valnil,而在于val.y才是nil。请参见此示例:

> x=nil
> if x.y > 10 then print("test") end
stdin:1: attempt to index global  x  (a nil value)
stack traceback:
    stdin:1: in main chunk
    [C]: ?
> x={y=nil}
> if x.y > 10 then print("test") end
stdin:1: attempt to compare number with nil
stack traceback:
    stdin:1: in main chunk
    [C]: ?

此外,当您将val设置为nil时,这可能没有任何作用(我认为val是一个副本):

> t={"a", "b", "c", "d"}
> for i,val in ipairs(t) do print(i, val) end
1   a
2   b
3   c
4   d
> for i,val in ipairs(t) do if i==3 then print("delete", val); val=nil end end
delete  c
> for i,val in ipairs(t) do print(i, val) end
1   a
2   b
3   c
4   d

编辑:要从表中删除元素,您需要表。删除

> t[3]=nil
> for i,val in ipairs(t) do print(i, val) end
1   a
2   b
> t[3]="c"
> for i,val in ipairs(t) do print(i, val) end
1   a
2   b
3   c
4   d
> for i,val in ipairs(t) do if i==3 then print("delete", val); table.remove(t, i) end end
delete  c
> for i,val in ipairs(t) do print(i, val) end
1   a
2   b
3   d

JeffK的解决方案应该有效,但我认为它有效的原因不是因为他向后遍历列表,而是因为他设置了particles[I]=nil,而不是val=nil。如果运行<code>val=nil</code>,则仅将val的本地副本设置为nil,而不是表中的条目。

试试这个:

for i,val in ipairs(particles) do
    if(val.y > 160) then
        particles[i]:removeSelf()
        particles[i] = nil;
    end
end

我认为当ipairs迭代时,不允许您修改表的内容。我隐约记得读过我的Lua 5.1参考手册,但我现在似乎找不到它。当您将val设置为nil时,它会从粒子表中删除一个元素。

您可以尝试反向处理该表,因为您的函数正在对粒子表进行全面扫描,有条件地删除一些项目:

for x = #particles, 1, -1 do
    if particles[x].y > 160 then
        particles[x]:removeSelf()
        particles[x] = nil
    end
end




相关问题
Wrapping a Lua object for use in C++ with SWIG

Currently I know how to have C++ objects instantiated and passed around in Lua using SWIG bindings, what I need is the reverse. I am using Lua & C++ & SWIG. I have interfaces in C++ and ...

Can t lua_resume after async_wait?

I have some lua script that have some long running task like getting a web page so I make it yield then the C code handle get page job async, so the thread free to do other job and after a specify ...

How to remove a lua table entry by its key?

I have a lua table that I use as a hashmap, ie with string keys : local map = { foo = 1, bar = 2 } I would like to "pop" an element of this table identified by its key. There is a table.remove() ...

Lua plain string.gsub

I ve hit s small block with string parsing. I have a string like: footage/down/temp/cars_[100]_upper/cars_[100]_upper.exr and I m having difficulty using gsub to delete a portion of the string. ...

why do i get "attempt to call global require (a nil value)"?

I have 3 lua files, Init.lua, FreeCamera.lua and Camera.lua , init.lua calls require "Core.Camera.FreeCamera" Free Camera: module(...) require "Core.Camera.Camera" local M = {} FreeCamera = M M ...