如何删除具有相同名称的所有对象

时间:2013-09-10 18:59:03

标签: lua corona

如果我使用的话,如何删除所有具有相同名称的对象:

local screenGroup = self.view
local randomBad3 = function()
  badC3 = display.newImage("BCloud3-"..tostring(math.random(1, 12))..".png")
  badC3.x = math.random (0, 450); badC3.y = -50-- -50
  physics.addBody( badC3, { density=.3, bounce=0.3, friction=.3, radius=25, filter=badc3CollisionFilter } )
  badC3.name = "BCloud3"    
  badC3.isSensor = true
  badC3.rotation = math.random(-30,30) -- Rotate the object
  trans5 = transition.to( badC3, { time= math.random(yForB3A, yForB3B), y=600,  transition=easing.OutExpo } )
  badC3.gravityScale = 0.0
  local cleanup
  cleanup = function()
   if badC3 then
       if badC3.y >590 then
           badC3:removeSelf()
           badC3 = nil
       end
   end
end
Runtime:addEventListener("enterFrame", cleanup)
end
randomBadC3 = timer.performWithDelay( math.random(1000, 5000), randomBad3, 0 )

-

   if badC3 then
           badC3:removeSelf()
           badC3 = nil
   end

它只删除最后生成的那个,而不是全部

1 个答案:

答案 0 :(得分:2)

如果要删除整个组,请将所有图像添加到一个组中。

for i=1,#maingroup do
    if maingroup[i] then
        maingroup[i]:removeSelf();maingroup[i]=nil
    end
end
end