2013-06-29 192 views
0

無法從表中刪除條目。從表中刪除條目

這裏是我的代碼

dropItem = dropList[ math.random(#dropList) ] 
dropSomething[brick.index] = crackSheet:grabSprite(dropItem, true) 
dropSomething[brick.index].x = brick.x 
dropSomething[brick.index].y = brick.y 
dropSomething[brick.index].name = dropItem 
dropSomething[brick.index].type = "dropppedItems" 

碰撞

function bounce(event) 
     local item = event.other 
     if item.type == "dropppedItems" then 
      if item.name == "bomb" then 
       Lives = Lives - 1 
       LivesNum.text = tostring(Lives) 
      end 
     item:removeSelf(); 
     end  

end 

我已經試過:

item:removeSelf(); -- removes the whole table 
item = nil -- seems to do nothing, the object continue 
      -- to move and i still see the image 

我可以刪除從屏幕中的對象的唯一方法是通過隱藏它
transition.to(item, {time = 100, alpha = 0})

+0

什麼'event.other = nil'? – 8bitcartridge

+0

我只能假設你的函數必須位於事件監聽器的頂部,如果你不能添加另一個代碼 – DevfaR

回答

0

有物品對象應該是您的實際物品的副本。意思是,它不像一個指針。所以如果你想從表格中刪除一個項目,你應該在表格中找到它。

您可以通過修改代碼:

dropItem = dropList[ math.random(#dropList) ] 
dropSomething[brick.index] = crackSheet:grabSprite(dropItem, true) 
dropSomething[brick.index].x = brick.x 
dropSomething[brick.index].y = brick.y 
dropSomething[brick.index].name = dropItem 
dropSomething[brick.index].type = "dropppedItems" 
dropSomething[brick.index].id = brick.index 

function bounce(event) 
     local item = event.other 
     if item.type == "dropppedItems" then 
      if item.name == "bomb" then 
       Lives = Lives - 1 
       LivesNum.text = tostring(Lives) 
      end 
     dropSomething[item.id]:removeSelf(); 
     dropSomething[item.id] = nil 
     end  
end 

假設抖動功能可以達到dropSomething表