从表中删除条目。

无法从表中删除条目。

以下是我的代码

dropItem = dropList[ math.random( #dropList ) ]
dropSomething[brick.index] = crackSheet:grabSprite(dropItem, true)
dropSomething[brick.index].x = brick.x
dropSomething[brick.index].y = brick.y
dropSomething[brick.index].name = dropItem
dropSomething[brick.index].type = "dropppedItems"

碰撞

function bounce(event)
        local item = event.other
        if item.type == "dropppedItems" then
            if item.name == "bomb" then
                Lives = Lives - 1
                LivesNum.text = tostring(Lives)
            end
        item:removeSelf();
        end

end

我尝试过的:

item:removeSelf(); -- 删除整个表
item = nil -- 看起来什么也没做,对象仍然会移动
           -- 并且我仍然看到图像

我唯一能够从屏幕中删除对象的方法是用

transition.to(item, {time = 100, alpha = 0})隐藏它。

点赞
用户2186639
用户2186639

在那里,物品对象应该是您实际物品的副本。这意味着它不像指针那样工作。因此,如果您想从表中删除一个项目,应该在表内找到它。

您可以将代码修改如下:

dropItem = dropList[math.random(#dropList)]
dropSomething[brick.index] = crackSheet:grabSprite(dropItem, true)
dropSomething[brick.index].x = brick.x
dropSomething[brick.index].y = brick.y
dropSomething[brick.index].name = dropItem
dropSomething[brick.index].type = "droppedItems"
dropSomething[brick.index].id = brick.index

function bounce(event)
    local item = event.other
    if item.type == "droppedItems" then
        if item.name == "bomb" then
            Lives = Lives - 1
            LivesNum.text = tostring(Lives)
        end
        dropSomething[item.id]:removeSelf()
        dropSomething[item.id] = nil
    end
end

假设bounce函数可以访问dropSomething表。

2013-07-03 21:53:05
用户88888888
用户88888888

如果你要将对象从表格中移除,你可以使用函数'table.remove()',这个函数非常容易使用。

要实现这个功能,你有两种方法: 一种是使用自定义函数,另一种则不用。

我建议使用自定义函数。

首先,你可以像这样做:

removeFromTable = function(tbl, lookfor)
 for i,v in pairs(tbl)do
    if(v.Name==lookfor)then -- 根据你想要用作标识的东西进行更改
       table.remove(tbl, i);
    end;
 end;
end;

这适用于RBLX_Lua。我不确定它是否适用于其他lua版本。它需要你为表格中的每个对象都有一个标识“标签”,你可能已经有了,因为你正在制作一个带有物品的游戏。

希望这有所帮助。

2018-11-08 15:30:57