Different layers in Corona/Lua
I've got a question about layering images/buttons with Corona/Lua. If I create one button on开发者_如何转开发 top of another one and then click it, both buttons' events are triggered. How do I prevent this?
Thanks, Elliot Bonneville
EDIT: Here's how I create the buttons:
button1 = display.newImage("button1.png")
button1:addEventListener("tap", Button1Call)
button2 = display.newImage("button2.png")
button2:addEventListener("tap", Button2Call)
Return true from the event handling function. Touch events keep propagating through the listeners until handled; it's explained here:
http://developer.anscamobile.com/content/events-and-listeners#Touch_Events
Note that the event listeners must be listening for the same event. In other words, both listeners must be set on either "touch" or "tap". Literally last night I was tripped up by this; I had a button listening to "touch" and another image on top listening to "tap" and was wondering why the button was still receiving events.
Use return true
in the event handler where you handle the event to prevent further event propagation.
So, in your example, button2
will get the event first, since it's created last. If you handle the event in Button2Call
andreturn true
, Button1Call
won't see the event at all. If you return false
, or simply leave out the return
statement altogether, Button1Call
will get the event and can decide whether to handle it.
精彩评论