0

我收到此错误:

尝试索引?一个零值

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

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

3 回答 3

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)
于 2013-07-16T12:54:12.560 回答
2

如果您只想在对象到达 时将其移除,请对对象>H使用碰撞。

它比 the 便宜得多,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
于 2013-07-16T15:37:49.003 回答
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

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

于 2013-07-16T14:45:49.967 回答