2013-07-16 47 views
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) 
+0

是否有滴'下降[drops.numChildren]'進入?還是那個在桌子的盡頭?找出哪條線(以及循環的哪個迭代)導致錯誤,這也有助於調試。 –

回答

2

如果你只是想刪除一個對象時它達到>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 
2

的錯誤來自您的運行時,它試圖刪除已經被刪除的對象和運行時繼續運行。你想刪除到綁定的,你可以參考這個代碼屏幕的對象,如果這是你想做的事我在這裏取下滴組/表什麼,因爲我認爲這是沒有必要的

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

真棒,謝謝! –

+0

不,我再次檢查代碼,以確保如果丟棄的內存已從內存中刪除,但是當我將它們放入顯示組中時,組的大小正在增加。他們不會被刪除!你對此有何評論? –

+0

你如何插入他們並將他們從你的組中刪除?因爲我可能不知道哪裏出現問題,除非我看到您的代碼 – DevfaR

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 

保持編碼............. :)

相關問題