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

Lua 尝试删除不再使用的显示对象时出错,lua,coronasdk,Lua,Coronasdk,我得到这个错误: 尝试索引?零值 在更新函数的开头。原因可能是什么 --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

我得到这个错误:

尝试索引?零值

在更新函数的开头。原因可能是什么

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

错误来自运行时,它尝试删除已删除的对象,运行时继续运行。您是否试图删除一个到达屏幕边界的对象?如果这是您想要做的,您可以参考此代码。我在这里删除drops组/表,因为我认为这是不必要的

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)

错误来自运行时,它尝试删除已删除的对象,运行时继续运行。您是否试图删除一个到达屏幕边界的对象?如果这是您想要做的,您可以参考此代码。我在这里删除drops组/表,因为我认为这是不必要的

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)

我正在用另一种方式做这件事。这将确保每一个屏幕外的水滴都将被移除:

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

继续编码…………:)

我正在用另一种方式做这件事。这将确保每一个屏幕外的水滴都将被移除:

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

继续编码…………:)

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

它比
enterFrame
便宜得多,而且不需要在表中插入对象,它比带有for循环的
enterFrame
快得多。我认为效率更高

--[[
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

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

它比
enterFrame
便宜得多,而且不需要在表中插入对象,它比带有for循环的
enterFrame
快得多。我认为效率更高

--[[
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

是否有
drops[drops.numChildren]
drops中的条目?还是那张桌子的尽头?找出哪一行(以及循环的哪一次迭代)导致了错误也将有助于这里的调试。drops中是否有
drops[drops.numChildren]
条目?还是那张桌子的尽头?找出哪一行(以及循环的哪一次迭代)导致了错误也将有助于这里的调试。不,我再次检查了代码,以确保如果删除内存中的数据,但当我将它们放入显示组时,组的大小会增加。它们没有被移除!您对此有何看法?如何将其插入组中并从组中删除?因为我可能不知道问题发生在哪里,除非我看到你的代码。我只是将它们插入代码的createDrop函数中,当你删除drop in update时,它不应该被删除吗?我发现我没有将local置于drop上,这就是为什么它与生成的最后一个对象冲突的原因。我将重新编辑我的代码Nope,我再次检查了代码,以确保如果从内存中删除了拖放,但当我将它们放在显示组中时,组的大小会增加。它们没有被移除!您对此有何看法?如何将其插入组中并从组中删除?因为我可能不知道问题发生在哪里,除非我看到你的代码。我只是将它们插入你代码的createDrop函数中,当你删除drop-in更新时,它不应该被删除吗?我发现我没有将local放在drop上,这就是为什么它与生成的最后一个对象冲突的原因。我将重新编辑我的代码