Multilayered intersection with events

If you have two display objects overlapping with a touch event on both of them and then click on the over lapping area you will fire both of the functions. How could I prevent it so only the one thats on the top most layer gets fired while the one under it gets ignored?
example on what I mean
 

red = display.newRect(25,25,50,50) red:setFillColor(1,0,0) red.alpha = .5 green = display.newRect(50,50,50,50) green:setFillColor(0,1,0) green.alpha = .5 function touchRed(event) if event.phase == "began" then print("Red") end end function touchGreen(event) if event.phase == "began" then print("Green") end end red:addEventListener("touch",touchRed) green:addEventListener("touch",touchGreen)

as you see it will print red and green but as green is on top it should only print green

Try adding “return true” (without quotes) at the bottem of you touch functions. Out side the if event.phase == “began” then

There are others ways but I like this way :slight_smile:

function touchGreen(event)
if event.phase == “began” then
print(“Green”)
end
return true
end

I was unaware return true would fix it but I guess it makes sence.

If you want to know why check out this video (go 3 minutes in)
https://youtu.be/tdq5yqM1-Us

Try adding “return true” (without quotes) at the bottem of you touch functions. Out side the if event.phase == “began” then

There are others ways but I like this way :slight_smile:

function touchGreen(event)
if event.phase == “began” then
print(“Green”)
end
return true
end

I was unaware return true would fix it but I guess it makes sence.

If you want to know why check out this video (go 3 minutes in)
https://youtu.be/tdq5yqM1-Us