尝试移除不再使用的显示对象时出现错误。

我遇到了这个错误:

尝试索引?空值

update 函数的开头。可能是什么原因呢?

--创建物品掉落
local createDrop=function()
drop = display.newImage("drop.png")
drop.x=math.random(drop.width, W-drop.width); drop.y=-50
drop.type="drop"
drops:insert(drop)
physics.addBody(drop)
print(drops.numChildren)
end

local dropTimer = timer.performWithDelay ( 1000, createDrop, 0)

 --移除不在屏幕内的物品掉落
 local function update()
   for i=1,drops.numChildren do
      if(drops[i].y>H) then
        drops[i]:removeSelf()
        drops[i] = nil
      end
   end
 end

--Runtime:addEventListener("enterFrame", function() print(drop.y) end) --检查是否已删除屏幕外的物品掉落
  Runtime:addEventListener("enterFrame", update)
点赞
用户1682268
用户1682268

错误来自于您的运行时,它试图移除已经被移除的对象,而运行时仍在运行。如果您正在尝试删除到达屏幕边界的对象,可以参考以下代码。我在这里删除了水滴组/表,因为我认为这不是必要的。

local physics = require("physics")
physics.start()

local W = display.contentWidth
local H = display.contentHeight

local createDrop=function()
    local drop = display.newImage("drop.png")
    drop.x=math.random(drop.width, W-drop.width); drop.y=-50
    drop.type="drop"
    physics.addBody(drop)

    local function update()
        if(drop.y > H) then
            drop:removeSelf()
            drop = nil
            print("remove drop")
            Runtime:removeEventListener("enterFrame", update)
        end
    end
    Runtime:addEventListener("enterFrame", update)
end

local dropTimer = timer.performWithDelay ( 1000, createDrop, 0)
2013-07-16 12:54:12
用户1979583
用户1979583
我用另一种方式来做。这将确保屏幕外的每一滴都会被清除:

W = display.contentWidth H = display.contentHeight local drops = display.newGroup() local physics = require "physics" physics.start()

local drop = {} local dropIndex = 0

local function createDrop() dropIndex = dropIndex + 1 drop[dropIndex] = display.newImage("drop.png") drop[dropIndex].x = math.random(drop[dropIndex].width, W - drop[dropIndex].width) drop[dropIndex].y = -50 drop[dropIndex].type = "drop" drops:insert(drop[dropIndex]) physics.addBody(drop[dropIndex]) -- print(drops.numChildren) end local dropTimer = timer.performWithDelay(1000, createDrop, 0)

local function update() for i=1, #drop do if(drop[i] ~= nil and drop[i].y >= H) then drop[i]:removeSelf() drop[i] = nil print("removed... drop[" .. i .. "]") end end end timer.performWithDelay(10, update, 0) -- you can also use enterFrame event instead

```

继续编程............. :)

2013-07-16 14:45:49
用户1605727
用户1605727

如果你只想在物体达到 >H 时移除它,可以使用对象的碰撞器。

它比使用 enterFrame 更便宜,无需将物体插入表中,比在其中使用 for 循环的 enterFrame 更快。我敢说更有效率。

--[[
这是与您的“drop”对象发生碰撞的传感器,它会在碰撞时自动删除。

它的位置在 y = H+10
]]
local removeSensorPoint = display.newRect(0,H+10,W,2)
removeSensorPoint.alpha = 0
removeSensorPoint.type = "removeSensor"
physics.addBody(removeSensorPoint,"static",{isSensor = true})

local createDrop = function()
    drop = display.newImage("drop.png")
    drop.x = math.random(drop.width, W-drop.width); drop.y=-50
    drop.type = "drop"

    physics.addBody(drop)
    drop.collision = function(self,event)
        if event.phase == "began" and event.other.type == "removeSensor" then
            event.target:removeSelf()
        end
    end
    drop:addEventListener("collision")
    print(drops.numChildren)
end
2013-07-16 15:37:49