Different layers in Corona/Lua

时光总嘲笑我的痴心妄想 提交于 2019-12-12 19:02:19

问题


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)

回答1:


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.




回答2:


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.



来源:https://stackoverflow.com/questions/6114817/different-layers-in-corona-lua

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!