Corona/Lua 中的不同层
我有一个关于使用 Corona/Lua 分层图像/按钮的问题。如果我在另一个按钮之上创建一个按钮然后单击它,则两个按钮的事件都会被触发。我该如何防止这种情况?
谢谢,Elliot Bonneville
编辑:这是我创建按钮的方法:
button1 = display.newImage("button1.png")
button1:addEventListener("tap", Button1Call)
button2 = display.newImage("button2.png")
button2:addEventListener("tap", Button2Call)
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)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
从事件处理函数返回 true。触摸事件不断通过监听器传播,直到被处理;此处解释:
http://developer.anscamobile.com/content/events-and -listeners#Touch_Events
请注意,事件侦听器必须侦听同一事件。换句话说,两个侦听器必须设置为“触摸”或“点击”。昨晚我确实被这个绊倒了;我有一个按钮在听“触摸”,顶部的另一个图像在听“点击”,我想知道为什么该按钮仍在接收事件。
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.
在处理事件的事件处理程序中使用
return true
以防止进一步的事件传播。因此,在您的示例中,
button2
将首先获取事件,因为它是最后创建的。如果您在Button2Call
中处理该事件并返回 true
,则Button1Call
将根本看不到该事件。如果您返回 false
,或者干脆省略return
语句,Button1Call
将获取事件并决定是否处理该事件。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 inButton2Call
andreturn true
,Button1Call
won't see the event at all. If youreturn false
, or simply leave out thereturn
statement altogether,Button1Call
will get the event and can decide whether to handle it.