Removing the object and event listener after timer is done

I am implementing a simple game where I will show blue or red box randomly. I have attached an event listener these boxes. However, when I draw these boxes, all of them get stacked above and it does not works as expected. Consider the code given below:

local function positiveCount( event ) correct\_tap = correct\_tap + 1 positive.text = correct\_tap end local function negativeCount( event ) incorrect\_tap = incorrect\_tap + 1 negative.text = incorrect\_tap end local function drawRect() print("drawing rect") local type = "" if(math.random() \< 0.5) then type='red' else type='blue' end if(type == 'red') then local redbox = display.newRoundedRect(display.contentCenterX, display.contentCenterY, 100, 100, 12) redbox:setFillColor(1,0,0) redbox:addEventListener("tap", positiveCount) else local bluebox = display.newRoundedRect(display.contentCenterX, display.contentCenterY, 100, 100, 12) bluebox:setFillColor(0,0,1) bluebox:addEventListener("tap", negativeCount) end end local function startGame(event) print("game started") local timegot = math.random(min\_value, max\_value) print("time ", timegot) timer.performWithDelay(timegot \* 1000, drawRect, 10) end

drawRect function randomly draws a blue or red box. Once the box is drawn, user can click on it to increase score. However, when another box is drawn, it looks like the new box is stacked upon earlier box and when clicked, all of these boxes event listener is called. I thought on the next iteration of performWithDealay, these objects were removed, but its not the case. How do I display the box for certain delay and remove it during the next iteration of timer.performWithDelay. Thanks.

Please (re-) read the tap / touch / multitouch guide.

You will notice it mentions the concept of event propagation

Once you understand this concept and how to control it, you will understand how to solve the problem you’re having.

Please (re-) read the tap / touch / multitouch guide.

You will notice it mentions the concept of event propagation

Once you understand this concept and how to control it, you will understand how to solve the problem you’re having.