Why touch event of bacground through objects above?

Hey guys!

I’ve got a touch event related to background. When I put objects above the event still fires even when Im only touch the objects above. I prepared the obects over the background with .toFront()-function. I am bogged down. :slight_smile: Someone could help?

Thanks in advance!

Touch events will always propagate down the the lowest object unless you tell it not to.  So for each objects touch event you need to add:

return true

to the end of the event listener.  Returning true tells it that you are done handling the touch event and don’t send it to others.  

Hey! Thanks so far - but I mean this:

--main.lua local visualObjcects = display.newGroup() local function doSomething( event ) if event.phase == "began" then print("Ooops! Background touched through an object above!") end return true end local background = display.newRect(0, 0, display.contentWidth, display.contentHeight); background:setFillColor(255, 255, 255, 255); visualObjcects:insert(background); background:toBack(); local above = display.newRect(0, 0, 50, 50); above:setFillColor(255, 0, 0, 255); above:setReferencePoint(display.CenterReferencePoint); above.x = display.contentWidth\*0.5; above.y = display.contentHeight\*0.5; visualObjcects:insert(above); above:toFront(); background:addEventListener("touch", doSomething)

“above” doesn’t have an event listener. Only your background does.

Some of my objects dont need an event listener. So I have to code one with nothing to do? Thats the way?

Thanks a lot!

Yes, if you don’t want the background to receive the touches then you to add the event listeners to the other objects like this:

local function onObjTouch(e)
return true
end

You only need this one function, and you need to add the same event listener to each object like this

obj1:addEventListener(“touch”, onObjTouch)
obj2:addEventListener(“touch”, onObjTouch)

As written above. Touch/tap event is always propagated through objects on top to objects on the bottom (depth) until you tell Corona to stop it (by returning true). So if you do not want event to propagate further then you must stop propagation chain by giving object listeners which at least return true.

Thanks a lot guys!!! Thats it! :slight_smile:

You may want to use “obj:toFront()” in your handler to make sure that, once touched, the object stays in focus.  Otherwise, if you drag one object over another, you may generate a new touch event on the second object.

Touch events will always propagate down the the lowest object unless you tell it not to.  So for each objects touch event you need to add:

return true

to the end of the event listener.  Returning true tells it that you are done handling the touch event and don’t send it to others.  

Hey! Thanks so far - but I mean this:

--main.lua local visualObjcects = display.newGroup() local function doSomething( event ) if event.phase == "began" then print("Ooops! Background touched through an object above!") end return true end local background = display.newRect(0, 0, display.contentWidth, display.contentHeight); background:setFillColor(255, 255, 255, 255); visualObjcects:insert(background); background:toBack(); local above = display.newRect(0, 0, 50, 50); above:setFillColor(255, 0, 0, 255); above:setReferencePoint(display.CenterReferencePoint); above.x = display.contentWidth\*0.5; above.y = display.contentHeight\*0.5; visualObjcects:insert(above); above:toFront(); background:addEventListener("touch", doSomething)

“above” doesn’t have an event listener. Only your background does.

Some of my objects dont need an event listener. So I have to code one with nothing to do? Thats the way?

Thanks a lot!

Yes, if you don’t want the background to receive the touches then you to add the event listeners to the other objects like this:

local function onObjTouch(e)
return true
end

You only need this one function, and you need to add the same event listener to each object like this

obj1:addEventListener(“touch”, onObjTouch)
obj2:addEventListener(“touch”, onObjTouch)

As written above. Touch/tap event is always propagated through objects on top to objects on the bottom (depth) until you tell Corona to stop it (by returning true). So if you do not want event to propagate further then you must stop propagation chain by giving object listeners which at least return true.

Thanks a lot guys!!! Thats it! :slight_smile:

You may want to use “obj:toFront()” in your handler to make sure that, once touched, the object stays in focus.  Otherwise, if you drag one object over another, you may generate a new touch event on the second object.