简体   繁体   English

Corona/Lua 中的不同层

[英]Different layers in Corona/Lua

I've got a question about layering images/buttons with Corona/Lua.我有一个关于使用 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.从事件处理 function 返回 true。 Touch events keep propagating through the listeners until handled;触摸事件一直通过侦听器传播,直到被处理; it's explained here:它在这里解释:

http://developer.anscamobile.com/content/events-and-listeners#Touch_Events 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.在您处理事件的事件处理程序中使用return true以防止进一步的事件传播。

So, in your example, button2 will get the event first, since it's created last.因此,在您的示例中, button2将首先获取事件,因为它是最后创建的。 If you handle the event in Button2Call and return true , Button1Call won't see the event at all.如果您在Button2Call中处理事件并return true ,则Button1Call根本看不到该事件。 If you return false , or simply leave out the return statement altogether, Button1Call will get the event and can decide whether to handle it.如果您return false ,或者干脆完全省略return语句, Button1Call将获取事件并决定是否处理它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM