Problem with event propagation on touch listeners?

Not sure if there is a way to stop propagation inside an event handler.

In the code below I create a box and add a event listener. Then inside the listener

I create another box at the same position add an event listener to it.

What is crazy is that after the first box is deleted it event gets propagated to

an object that did not even exist when the event began. 

function makeBox(name)

  local box = display.newRect(50,50,50,50)

  box.name=name

  boxCount = boxCount + 1

  return box

end

local box1 = makeBox(“box1”)

local box2

function box1EventFunc(event) 

  print(event.target.name, event.phase)

  box1:removeEventListener(“touch”, box1EventFunc)

  box1:removeSelf() 

  box2 = makeBox(“box2”) 

  box2:addEventListener(“touch”, function(event) print(“box2”, event.phase) end)

  return true 

end

box1:addEventListener(“touch”, box1EventFunc)

A timer.performWithDelay for maybe 10 ms might do the trick. But as for the actual propagation, it is only stopped when you return true, so there is no way for you create box2 after returning true, unfortunately.

Try this instead:

local function onTouch( self, event) print(event.target.name, event.phase) return true end local function makeBox( name ) local box = display.newRect(50,50,50,50) box.name = name box.touch = onTouch box:addEventListener("touch") return box end local box1 = makeBox("box1") local box2 = makeBox("box1") display.remove( box1 ) -- Now touch box 2

Thank you for the responses,

atrizhong,

        That works, i already do that, but its more of a temporary workaround (hack). What I really would like to know

        is why the event is propagated to an element that wasn’t even around at the time the event was started. 

         

        roaminggamer

       That is another way to do creation of two boxes with events but not a relevant solution to the issue.

     

In our program we have dynamically generated UI elements. Those elements are created as a result 

of a button press. If the UI element is added at the same location as the button it will then receive the button press event

as if the user touched the element even before the original button press event was resolved.

I already know a work around simply involves delaying the creation of the new element until after the button press resolves.

What I am wondering is, if this is a bug or is it intentional? and if so why?

So I figured out what was going wrong.

While I still question the logic behind why this sort of propagation can even occur. I found a natural way to avoid the problem.

As it turns out I was not using 

display.getCurrentStage():setFocus(event.target) 

during the began phase and 

display.getCurrentStage():setFocus(nil)

during the ended or cancelled phase

This stops the events from propagating to newly created objects inside an event handler.

A timer.performWithDelay for maybe 10 ms might do the trick. But as for the actual propagation, it is only stopped when you return true, so there is no way for you create box2 after returning true, unfortunately.

Try this instead:

local function onTouch( self, event) print(event.target.name, event.phase) return true end local function makeBox( name ) local box = display.newRect(50,50,50,50) box.name = name box.touch = onTouch box:addEventListener("touch") return box end local box1 = makeBox("box1") local box2 = makeBox("box1") display.remove( box1 ) -- Now touch box 2

Thank you for the responses,

atrizhong,

        That works, i already do that, but its more of a temporary workaround (hack). What I really would like to know

        is why the event is propagated to an element that wasn’t even around at the time the event was started. 

         

        roaminggamer

       That is another way to do creation of two boxes with events but not a relevant solution to the issue.

     

In our program we have dynamically generated UI elements. Those elements are created as a result 

of a button press. If the UI element is added at the same location as the button it will then receive the button press event

as if the user touched the element even before the original button press event was resolved.

I already know a work around simply involves delaying the creation of the new element until after the button press resolves.

What I am wondering is, if this is a bug or is it intentional? and if so why?

So I figured out what was going wrong.

While I still question the logic behind why this sort of propagation can even occur. I found a natural way to avoid the problem.

As it turns out I was not using 

display.getCurrentStage():setFocus(event.target) 

during the began phase and 

display.getCurrentStage():setFocus(nil)

during the ended or cancelled phase

This stops the events from propagating to newly created objects inside an event handler.