尝试删除不再使用的显示对象时出错

时间:2013-07-16 11:51:36

标签: lua corona

我收到此错误:尝试索引?零值

在更新功能的开头。可能是什么原因?

--Create drop
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)

 --Remove the drops that is out of screen
 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) --to check if the drop that is out off screen is removed.
  Runtime:addEventListener("enterFrame", update)

3 个答案:

答案 0 :(得分:2)

错误来自您的运行时它尝试删除已经删除的对象并且运行时继续运行。你试图删除一个到达屏幕边界的对象你可以参考这个代码如果这是你想要做的我在这里删除了drop组/表因为我认为没有必要

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)

答案 1 :(得分:2)

如果您只想在对象到达>H时删除它,请对该对象使用碰撞。

它比enterFrame便宜得多,你不需要在表中插入对象,它比enterFrame中的for循环要快得多。我会说更有效率。

--[[
This is the sensor that will collide to your "drop" object and it will automatically remove
itself upon collision.

It is positioned in 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

答案 2 :(得分:0)

我是以另一种方式做的。这将确保每次屏幕掉落都会被删除:

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

保持编码.............:)