How to remove objects after it spawns...

I cant remove object ( icon , icon2) after it spawns 

[lua]local mRandom = math.random
local mABs = math.abs

local objects = { “icon”, “icon2” }

local function spawnObject ()

local objIdx = mRandom(#objects)
local objName = objects[objIdx]
local ab = display.newImage (""…objName…".png")
ab.x = mRandom(40,300)
ab.y = display.contentCenterY

physics.addBody (ab, “dynamic”)
end

timer.performWithDelay ( 300, spawnObject, 3)

local btn = display.newRect ( 10, 10, 70, 70)
btn.x = display.contentCenterX - 50
btn.y = display.contentCenterY - -180

function removeicon (event)

if event.phase == “ended” then

display.remove ( ab )

end
end

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

Thanks

Hi xming0529,

You need to declare local btn outside of the spawnObject function and write

btn = display.newRect(10,10,70,70) in spawnObject function.

Best Regards,

Team, SP Technolab

www.sptechnolab.com

What i did was copy the btn to the function, when i set the onComplete thing, the icon double ups every time i press the btn

1 -> 2 -> 4 -> 8 -> 16 ( icon , icon 2)

[lua]local mRandom = math.random
local mABs = math.abs

local objects = { “icon”, “icon2” }

local function spawnObject ()
local objIdx = mRandom(#objects)
local objName = objects[objIdx]
local ab = display.newImage (""…objName…".png")
ab.x = mRandom(40,300)
ab.y = display.contentCenterY

physics.addBody (ab, “dynamic”)

local btn = display.newRect ( 10, 10, 70, 70)
btn.x = display.contentCenterX - 50
btn.y = display.contentCenterY - -180

function removeicon (event)
if event.phase == “ended” then
display.remove ( ab )
onComplete = spawnObject ()
end
end

btn:addEventListener (“touch”, removeicon)
end

timer.performWithDelay ( 300, spawnObject, 1)[/lua]

Hi xming0529,

You need to declare local btn outside of the spawnObject function and write

btn = display.newRect(10,10,70,70) in spawnObject function.

Best Regards,

Team, SP Technolab

www.sptechnolab.com

What i did was copy the btn to the function, when i set the onComplete thing, the icon double ups every time i press the btn

1 -> 2 -> 4 -> 8 -> 16 ( icon , icon 2)

[lua]local mRandom = math.random
local mABs = math.abs

local objects = { “icon”, “icon2” }

local function spawnObject ()
local objIdx = mRandom(#objects)
local objName = objects[objIdx]
local ab = display.newImage (""…objName…".png")
ab.x = mRandom(40,300)
ab.y = display.contentCenterY

physics.addBody (ab, “dynamic”)

local btn = display.newRect ( 10, 10, 70, 70)
btn.x = display.contentCenterX - 50
btn.y = display.contentCenterY - -180

function removeicon (event)
if event.phase == “ended” then
display.remove ( ab )
onComplete = spawnObject ()
end
end

btn:addEventListener (“touch”, removeicon)
end

timer.performWithDelay ( 300, spawnObject, 1)[/lua]