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