【问题标题】:Getting error while trying to remove display objects that are no use anymore尝试删除不再使用的显示对象时出错
【发布时间】:2013-07-16 23:37:49
【问题描述】:

我收到此错误:

尝试索引?一个零值

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

--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 中有drops[drops.numChildren] 条目吗?还是那一个超出了桌子的尽头?找出导致错误的行(以及循环的迭代)也有助于此处的调试。

标签: lua coronasdk


【解决方案1】:

如果您只想在对象到达>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】:

    错误来自您的运行时,它尝试删除已被删除的对象并且运行时继续运行。您是否要删除到达屏幕边界的对象如果这是您想要执行的操作,您可以参考此代码我在此处删除 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)
    

    【讨论】:

    • 不,我再次检查了代码以确保如果从内存中删除了drop,但是当我将它们放入显示组时,组的大小正在增加。他们没有被删除!你对此有何看法?
    • 您如何将它们插入您的组并从您的组中删除它们?因为除非我看到你的代码,否则我可能不知道问题出在哪里
    • 我只是将它们插入到代码的 createDrop 函数中,不应该在删除 drop in update 时将其删除吗?
    • 我发现我没有将本地放在 drop 这就是为什么它与最后一个生成的对象冲突我将重新编辑我的代码
    【解决方案3】:

    我正在以另一种方式做到这一点。这将确保每一个屏幕外掉落都会被移除:

    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
    

    继续编码...... :)

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-07-07
      • 2018-08-20
      • 1970-01-01
      • 2021-07-23
      • 1970-01-01
      • 2018-02-05
      • 2021-08-27
      • 2021-05-27
      相关资源
      最近更新 更多