The following is a discussion about what should happen when you delete a control from inside its touch listener.
take for instance the following code:
local r1 = display.newRect(300,300,300,300) r1:setFillColor(1,0,0,1) local r2 = display.newRect(300,300,100,100) r1:addEventListener("touch", function(event) print("r1 touch event", event.phase) if(event.phase=="ended" or event.phase=="cancelled")then event.target.focus=false display.getCurrentStage():setFocus(nil) elseif(event.phase=="began")then event.target.focus = true display.getCurrentStage():setFocus(event.target) end return true end) r2:addEventListener("touch", function(event) print("r2 touch event", event.phase) if(event.phase=="ended" or event.phase=="cancelled")then event.target.focus=false display.getCurrentStage():setFocus(nil) elseif(event.phase=="began")then event.target.focus = true display.getCurrentStage():setFocus(event.target) r2:removeSelf() r2 = nil end return true end)
If you touch the white square you will get a print out of the events that are handled by each rectangles touch listener.
Normally events propagate from top to bottom unless your listener returns true in which case the event is consumed and not propagated to other objects underneath.
In this case we delete the control in the middle of handling its ended event. Therefore we do not return true.
As you can see there is never an “ended” event fired for the r2 rectangle and yet an “ended” event is still propagated to the object below (r1).
Why does this matter?
In my application I have two overlapping menus. The top menu has a close button. When I press the button its deletes the top menu and would normally stop the touch event. However because of the behavior described above it propagates the event to the menu below calling its close menu as well. In case of other menus it may cause undesired interaction with controls on menu below the one being closed.
Should it be events from chaining at the moment that the object is deleted?