How to remove an object that share the same name?

[lua]centerX = display.contentCenterX
centerY = display.contentCenterY

local mRandom = math.random

local ground = display.newRect ( 10, 10, 320, 10)
ground.x = centerX
ground.y = centerY - -180
physics.addBody ( ground, “static”, {friction = 0.3, bounce = 0.1} )

function newBox ()

rand = mRandom ( 3 )

 

if ( rand < 2 ) then

a = display.newRect ( 10, 10, 50, 50)
a.x = centerX
a.y = centerY - 100
a:setFillColor(155,0,0)
physics.addBody ( a, " static", {density = 0.1, friction = 3, bounce = 0} )

else if
( rand < 3 ) then
b = display.newRect ( 10, 10, 50, 50)
b.x = centerX
b.y = centerY - 100
b:setFillColor(155,155,0)
physics.addBody ( b, " static", {density = 0.1, friction = 3, bounce = 0} )

else if
( rand < 4 ) then
c = display.newRect ( 10, 10, 50, 50)
c.x = centerX
c.y = centerY - 100
c:setFillColor(0,155,0)
physics.addBody ( c, " static", {density = 0.1, friction = 3, bounce = 0} )

end
end
end
end

timer.performWithDelay ( 300, newBox, 3)
 

local Rbtn = display.newRect( 150, 450, 30, 30 )
Rbtn.x = display.contentCenterX - 130
Rbtn:setFillColor(155,0,0)

local Ybtn = display.newRect( 160, 450, 30, 30 )
Ybtn.x = display.contentCenterX
Ybtn:setFillColor(155,155,0)

local Gbtn = display.newRect( 220, 450, 30, 30 )
Gbtn.x = display.contentCenterX - -130
Gbtn:setFillColor(0,155,0)

function touchRedbtn ( event )

if event.phase == “began” then
display.remove (a)
a = nil
onComplete = newBox()

return true
end
end

function touchYellowbtn ( event )

if event.phase == “began” then
display.remove ( B)
b = nil
onComplete = newBox()

return true
end
end

function touchGreenbtn ( event )

if event.phase == “began” then
display.remove ©
c = nil
onComplete = newBox()

return true
end
end

Rbtn:addEventListener (“touch”, touchRedbtn)

Ybtn:addEventListener (“touch”, touchYellowbtn)
Gbtn:addEventListener (“touch”, touchGreenbtn)
[/lua]

when i press the red / green / yellow button, it randomly remove 1 of them in the screen,

my question is --> How to set to remove the most bottom box and not affecting all other boxes that on top of the box

Try something like this (may have a few syntax errors, untested):

local physics = require "physics" local centerX = display.contentCenterX local centerY = display.contentCenterY local mRandom = math.random local ground = display.newRect ( 10, 10, 320, 10) ground.x = centerX ground.y = centerY - -180 physics.addBody ( ground, "static", {friction = 0.3, bounce = 0.1} ) -- Create tables to store new boxes in local redboxes = {} local yellowboxes = {} local greenboxes = {} -- Define new box creator function local function newBox () local rand = mRandom ( 3 ) if ( rand \< 2 ) then local tmp = display.newRect ( 10, 10, 50, 50) tmp.x = centerX tmp.y = centerY - 100 tmp:setFillColor(155,0,0) physics.addBody ( a, " static", {density = 0.1, friction = 3, bounce = 0} ) redboxes[#redboxes+1] = tmp elseif ( rand \< 3 ) then local tmp = display.newRect ( 10, 10, 50, 50) tmp.x = centerX tmp.y = centerY - 100 tmp:setFillColor(155,155,0) physics.addBody ( tmp, " static", {density = 0.1, friction = 3, bounce = 0} ) yellowboxes[#yellowboxes+1] = tmp elseif ( rand \< 4 ) then local tmp = display.newRect ( 10, 10, 50, 50) tmp.x = centerX tmp.y = centerY - 100 tmp:setFillColor(0,155,0) physics.addBody ( tmp, " static", {density = 0.1, friction = 3, bounce = 0} ) greenboxes[#greenboxes+1] = tmp end end -- Define single onTouch table listener local function onTouch( self, event ) local boxes = self.boxes if(#boxes == 0) then return true end if event.phase == "began" then display.remove[boxes[1]] table.remove(boxes,1) newBox() end return true end -- Create 3 buttons all using the same event listener -- local Rbtn = display.newRect( 150, 450, 30, 30 ) Rbtn.x = display.contentCenterX - 130 Rbtn:setFillColor(155,0,0) Rbtn.touch = onTouch Rbtn.boxes = redboxes -- assign my box table to me Rbtn:addEventListener ("touch" ) local Ybtn = display.newRect( 160, 450, 30, 30 ) Ybtn.x = display.contentCenterX Ybtn:setFillColor(155,155,0) Ybtn.touch = onTouch Ybtn.boxes = yellowboxes -- assign my box table to me Ybtn:addEventListener ("touch" ) local Gbtn = display.newRect( 220, 450, 30, 30 ) Gbtn.x = display.contentCenterX - -130 Gbtn:setFillColor(0,155,0) Gbtn.touch = onTouch Gbtn.boxes = greenboxes -- assign my box table to me Gbtn:addEventListener ("touch" ) -- Create 3 new boxes in 300 ms timer.performWithDelay ( 300, newBox, 3)

Thanks for your help. It inspires me alot

Try something like this (may have a few syntax errors, untested):

local physics = require "physics" local centerX = display.contentCenterX local centerY = display.contentCenterY local mRandom = math.random local ground = display.newRect ( 10, 10, 320, 10) ground.x = centerX ground.y = centerY - -180 physics.addBody ( ground, "static", {friction = 0.3, bounce = 0.1} ) -- Create tables to store new boxes in local redboxes = {} local yellowboxes = {} local greenboxes = {} -- Define new box creator function local function newBox () local rand = mRandom ( 3 ) if ( rand \< 2 ) then local tmp = display.newRect ( 10, 10, 50, 50) tmp.x = centerX tmp.y = centerY - 100 tmp:setFillColor(155,0,0) physics.addBody ( a, " static", {density = 0.1, friction = 3, bounce = 0} ) redboxes[#redboxes+1] = tmp elseif ( rand \< 3 ) then local tmp = display.newRect ( 10, 10, 50, 50) tmp.x = centerX tmp.y = centerY - 100 tmp:setFillColor(155,155,0) physics.addBody ( tmp, " static", {density = 0.1, friction = 3, bounce = 0} ) yellowboxes[#yellowboxes+1] = tmp elseif ( rand \< 4 ) then local tmp = display.newRect ( 10, 10, 50, 50) tmp.x = centerX tmp.y = centerY - 100 tmp:setFillColor(0,155,0) physics.addBody ( tmp, " static", {density = 0.1, friction = 3, bounce = 0} ) greenboxes[#greenboxes+1] = tmp end end -- Define single onTouch table listener local function onTouch( self, event ) local boxes = self.boxes if(#boxes == 0) then return true end if event.phase == "began" then display.remove[boxes[1]] table.remove(boxes,1) newBox() end return true end -- Create 3 buttons all using the same event listener -- local Rbtn = display.newRect( 150, 450, 30, 30 ) Rbtn.x = display.contentCenterX - 130 Rbtn:setFillColor(155,0,0) Rbtn.touch = onTouch Rbtn.boxes = redboxes -- assign my box table to me Rbtn:addEventListener ("touch" ) local Ybtn = display.newRect( 160, 450, 30, 30 ) Ybtn.x = display.contentCenterX Ybtn:setFillColor(155,155,0) Ybtn.touch = onTouch Ybtn.boxes = yellowboxes -- assign my box table to me Ybtn:addEventListener ("touch" ) local Gbtn = display.newRect( 220, 450, 30, 30 ) Gbtn.x = display.contentCenterX - -130 Gbtn:setFillColor(0,155,0) Gbtn.touch = onTouch Gbtn.boxes = greenboxes -- assign my box table to me Gbtn:addEventListener ("touch" ) -- Create 3 new boxes in 300 ms timer.performWithDelay ( 300, newBox, 3)

Thanks for your help. It inspires me alot