One Quick Question About Removing a spawned object

Hi,  :mellow:

I have added a timer.performwithdelay to a function to spawn objects (Box)  1 by 1 in every 2 secs in  a random y point.

[lua]------Spawn Box Function
function spawnBox ()

     height = math.random ( display.contentCenterY - 160, display.contentCenterY + 160)

     Box = display.newImageRect ( “Redbox.png”, 110, 350)
     Box.x = display.contentCenterX
     Box.y = height

end

------Spawn with delay
timer.performWithDelay ( 2000, spawnBox, -1)[/lua]

Also, I created a Btn to remove the spawn objects

[lua]------ Create btn

local btn = display.newRect ( 100, 100, 50, 50 )

function removeBox ()

     if event.phase == “began” then
          display.remove (Box)
     end
end

btn:addEventListener (“touch”, removeBox)[/lua]

However, I can’t remove the first one when the objects spawn more than 2.

( The Btn can only remove the latest spawned object )

Thanks For Reading and answering my question

Firstly you need to look at the docs for local vs global variables, you’re creating a lot of global variables in your code which will only cause problems later.

This code will remove the last added box:

[lua]

local btn = display.newRect ( 100 , 100 , 50 , 50 )

local boxes = {}

       

local function removeBox (event)

      if event.phase == “began” then

         

            local lastBox = boxes[#boxes]

                 

            if lastBox ~= nil then

                  display.remove(lastBox)

                  table.remove(boxes, #boxes)

            end

      end

end

       

       

btn:addEventListener(“touch”,removeBox)

local function spawnBox ()

      local height = math.random ( display.contentCenterY - 160 , display.contentCenterY + 160 )

      local Box = display.newImageRect ( “Redbox.png”, 110 , 350 )

      Box.y = height

      boxes[#boxes+ 1] = Box

end

------Spawn with delay

timer.performWithDelay ( 2000 , spawnBox, - 1 )

[/lua]

Thanks, 

So, What if i want to remove the box from the first one to the latest one?

what it spawned was box1, box2, box3, box4… But what I’m trying to do is to remove the box1, then box2, instead of box4 then box3

Change the bit that removes the boxes to:

[lua]

 

local firstBox = boxes[1]

if firstBox ~= nil then

      display.remove(firstBox)

      table.remove(boxes, 1)

end

 

[/lua]

 

Firstly you need to look at the docs for local vs global variables, you’re creating a lot of global variables in your code which will only cause problems later.

This code will remove the last added box:

[lua]

local btn = display.newRect ( 100 , 100 , 50 , 50 )

local boxes = {}

       

local function removeBox (event)

      if event.phase == “began” then

         

            local lastBox = boxes[#boxes]

                 

            if lastBox ~= nil then

                  display.remove(lastBox)

                  table.remove(boxes, #boxes)

            end

      end

end

       

       

btn:addEventListener(“touch”,removeBox)

local function spawnBox ()

      local height = math.random ( display.contentCenterY - 160 , display.contentCenterY + 160 )

      local Box = display.newImageRect ( “Redbox.png”, 110 , 350 )

      Box.y = height

      boxes[#boxes+ 1] = Box

end

------Spawn with delay

timer.performWithDelay ( 2000 , spawnBox, - 1 )

[/lua]

Thanks, 

So, What if i want to remove the box from the first one to the latest one?

what it spawned was box1, box2, box3, box4… But what I’m trying to do is to remove the box1, then box2, instead of box4 then box3

Change the bit that removes the boxes to:

[lua]

 

local firstBox = boxes[1]

if firstBox ~= nil then

      display.remove(firstBox)

      table.remove(boxes, 1)

end

 

[/lua]