remove all objects with the same name

Hi how can i remove all objects with the same name, if i use: 

    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

it only removes the last spawned one, not all of them

When you call a new spawn badC3, you lose the reference to the old one. You need to use tables to store groups of common objects.

[lua]

local badC3 = {}

badC3[#badC3+1] = whatever – spawn new object and add to table

[/lua]

then to delete all objects…

[lua]

for i = #badC3, 1, -1 do

    if badC3[i] ~= nil then

        badC3[i]:removeSelf()

        badC3[i] = nil

    end

end

[/lua]

Hi Nick,

so i have done :

local badC3T = {}

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[#badC3T+1] = whatever3       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 )  

function pontsMin20() for i = #badC3T, 1, -1 do     if badC3T[i] ~= nil then         badC3[i]:removeSelf()         badC3[i] = nil print("hello")     end end end

local function onCollision(event)     if event.phase == "began" and gameIsActive == true then         local obj1 = event.object1;          local obj2 = event.object2;          if obj1.name == "jetplayer" then         if  obj2.name == "BCloud3" then pontsMin20()         end     end

but its not removing the objects in the tables not quite sure why that is… ps the “print(“hello”)” is not showing in the terminal  

I corrected your code, sorry I haven’t got time to explain the changes or test it :slight_smile:

[lua]

local badC3T = {}

local screenGroup = self.view

local cleanup = function ()

    for i = #badC3T, 1, -1 do

        if badC3T[i] ~= nil then

            if badC3T[i].y > 590 then

                badC3T[i]:removeSelf()

                badC3T[i] = nil

            end

        end

    end

end

local randomBad3 = function()

    local i = display.newImage(“BCloud3-”…tostring(math.random(1, 12))…".png")

    i.x = math.random (0, 450); i.y = -50-- -50

    physics.addBody( i, { density=.3, bounce=0.3, friction=.3, radius=25, filter=badc3CollisionFilter } )

    i.name = “BCloud3”

    i.isSensor = true

    i.rotation = math.random(-30,30) – Rotate the object

    transition.to( i, { time= math.random(yForB3A, yForB3B), y=600,  transition=easing.OutExpo } )

    i.gravityScale = 0.0

    badC3T[#badC3T+1] = i

end

function pontsMin20()

    for i = #badC3T, 1, -1 do

        if badC3T[i] ~= nil then

            badC3T[i]:removeSelf()

            badC3T[i] = nil

        end

    end

end

local function onCollision(event)

    if event.phase == “began” and gameIsActive == true then

        local obj1 = event.object1;

        local obj2 = event.object2;

        if obj1.name == “jetplayer” then

            if  obj2.name == “BCloud3” then pontsMin20(); end

        end

end

local randomBadC3 = timer.performWithDelay( math.random(1000, 5000), randomBad3, 0 )

Runtime:addEventListener( “collision”, onCollision )

Runtime:addEventListener(“enterFrame”, cleanup)

[/lua]

Should be 

[lua]

 badC3T[#badC3T+1] = whatever3

[/lua]

@nick & piotrz55

i see you have to do the “1.” to all of the function :smiley:

one more thins im trying to dress it up a bit, but getting this error : Attempt to index field ‘_target’ (a number value)

    for i = #badC1T, 1, -1 do         if badC1T[i] ~= nil then         transition.to( i, {  time=500, alpha=0,} )         local function dellBadC1T()             badC1T[i]:removeSelf()             badC1T[i] = nil         end         timer.performWithDelay( 500, dellBadC1T, 1 )         end     end  

FIXED BY : transition.to( badC1T[i], { time=500, alpha=0,} ) 

Replace the timer line with your transition from line 3. Transitions have an optional onComplete parameter which you should use to remove it. So after the alpha = 0, add onComplete = dellBadC1T. And make sure you move the transition line to where the timer is.

  for i = #badC1T, 1, -1 do         if badC1T[i] ~= nil then                 local function dellBadC1T()             badC1T[i]:removeSelf()             badC1T[i] = nil         end         transition.to( i, {  time=500, alpha=0, onComplete = dellBadC1T} )         end     end  

Hi naveen_pcs still getting the error that way, but if i do this :

    for i = #badC1T, 1, -1 do         if badC1T[i] ~= nil then         transition.to( badC1T[i], {  time=500, alpha=0} )         local function dellBadC1T()             badC1T[i]:removeSelf()             badC1T[i] = nil         end         timer.performWithDelay( 500, dellBadC1T, 1 )         end     end  

it works :smiley:

When you call a new spawn badC3, you lose the reference to the old one. You need to use tables to store groups of common objects.

[lua]

local badC3 = {}

badC3[#badC3+1] = whatever – spawn new object and add to table

[/lua]

then to delete all objects…

[lua]

for i = #badC3, 1, -1 do

    if badC3[i] ~= nil then

        badC3[i]:removeSelf()

        badC3[i] = nil

    end

end

[/lua]

Hi Nick,

so i have done :

local badC3T = {}

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[#badC3T+1] = whatever3       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 )  

function pontsMin20() for i = #badC3T, 1, -1 do     if badC3T[i] ~= nil then         badC3[i]:removeSelf()         badC3[i] = nil print("hello")     end end end

local function onCollision(event)     if event.phase == "began" and gameIsActive == true then         local obj1 = event.object1;          local obj2 = event.object2;          if obj1.name == "jetplayer" then         if  obj2.name == "BCloud3" then pontsMin20()         end     end

but its not removing the objects in the tables not quite sure why that is… ps the “print(“hello”)” is not showing in the terminal  

I corrected your code, sorry I haven’t got time to explain the changes or test it :slight_smile:

[lua]

local badC3T = {}

local screenGroup = self.view

local cleanup = function ()

    for i = #badC3T, 1, -1 do

        if badC3T[i] ~= nil then

            if badC3T[i].y > 590 then

                badC3T[i]:removeSelf()

                badC3T[i] = nil

            end

        end

    end

end

local randomBad3 = function()

    local i = display.newImage(“BCloud3-”…tostring(math.random(1, 12))…".png")

    i.x = math.random (0, 450); i.y = -50-- -50

    physics.addBody( i, { density=.3, bounce=0.3, friction=.3, radius=25, filter=badc3CollisionFilter } )

    i.name = “BCloud3”

    i.isSensor = true

    i.rotation = math.random(-30,30) – Rotate the object

    transition.to( i, { time= math.random(yForB3A, yForB3B), y=600,  transition=easing.OutExpo } )

    i.gravityScale = 0.0

    badC3T[#badC3T+1] = i

end

function pontsMin20()

    for i = #badC3T, 1, -1 do

        if badC3T[i] ~= nil then

            badC3T[i]:removeSelf()

            badC3T[i] = nil

        end

    end

end

local function onCollision(event)

    if event.phase == “began” and gameIsActive == true then

        local obj1 = event.object1;

        local obj2 = event.object2;

        if obj1.name == “jetplayer” then

            if  obj2.name == “BCloud3” then pontsMin20(); end

        end

end

local randomBadC3 = timer.performWithDelay( math.random(1000, 5000), randomBad3, 0 )

Runtime:addEventListener( “collision”, onCollision )

Runtime:addEventListener(“enterFrame”, cleanup)

[/lua]

Should be 

[lua]

 badC3T[#badC3T+1] = whatever3

[/lua]

@nick & piotrz55

i see you have to do the “1.” to all of the function :smiley:

one more thins im trying to dress it up a bit, but getting this error : Attempt to index field ‘_target’ (a number value)

    for i = #badC1T, 1, -1 do         if badC1T[i] ~= nil then         transition.to( i, {  time=500, alpha=0,} )         local function dellBadC1T()             badC1T[i]:removeSelf()             badC1T[i] = nil         end         timer.performWithDelay( 500, dellBadC1T, 1 )         end     end  

FIXED BY : transition.to( badC1T[i], { time=500, alpha=0,} ) 

Replace the timer line with your transition from line 3. Transitions have an optional onComplete parameter which you should use to remove it. So after the alpha = 0, add onComplete = dellBadC1T. And make sure you move the transition line to where the timer is.

  for i = #badC1T, 1, -1 do         if badC1T[i] ~= nil then                 local function dellBadC1T()             badC1T[i]:removeSelf()             badC1T[i] = nil         end         transition.to( i, {  time=500, alpha=0, onComplete = dellBadC1T} )         end     end  

Hi naveen_pcs still getting the error that way, but if i do this :

    for i = #badC1T, 1, -1 do         if badC1T[i] ~= nil then         transition.to( badC1T[i], {  time=500, alpha=0} )         local function dellBadC1T()             badC1T[i]:removeSelf()             badC1T[i] = nil         end         timer.performWithDelay( 500, dellBadC1T, 1 )         end     end  

it works :smiley: