Reputation: 131
Let's say
-I have a button that listens to a "tap" event, and directs to a function that does something.
-I put an ImageRact that covers the button. One layer up.
When I click on the cover image just above the area the buttons lies behind , the event function STILL executes.
How do I avoid this?
example:
local function hidebg()
display.remove(logo3)
logo3=nil
end
local logo2= display.newImage("logo.png")
logo2.x=display.contentCenterX
logo2.y=280
logo2.width=200
logo2.height=74
logo2:addEventListener("tap", hidebg)
local cover =display.newImageRect("NEW GAME A.png", 480,320)
cover.x=display.contentCenterX/2
cover.y=display.contentCenterY/2
The hidebg() function is still executed although the "logo2" is covered by "cover" image.
I know I could make the button isVisible=false and solve the problem, but I have dozens of buttons in different groups in different layers, and I wonder how to do it in a smart way. Maybe somehow disable a whole group? I don't know.
Upvotes: 1
Views: 1222
Reputation: 1183
The reason that this problem can be solved by adding a touch event listener that returns true
to the masking DisplayObject, as suggested in the accepted answer, is that this handles or halts the propagation of the touch. Having been handled by the masking object, the touch will never reach the listener on the button located further down in the display hierarchy (or further back, if you prefer).
This is explained in the Corona SDK documentation on tap/touch propagation:
When the user touches the screen, the event is dispatched to the display hierarchy. Only those display objects that intersect the touch location on the screen will receive the event.
Tap and touch events propagate through these objects in a particular order. By default, the first object to receive the event is the front-most display object in the display hierarchy that intersects the touch location. The next object to receive the event is the next object back in the hierarchy that intersects the touch location, and so on.
Tap and touch events propagate until they are "handled." This means that if you have multiple objects overlaying each other in the display hierarchy, and a tap or touch event listener has been applied to each, the event will propagate through all of these objects. However, you can stop propagation to the next underlying object by telling Corona that the event has been handled. This is as simple as returning true [emphasis mine] from the event listener — this stops the propagation cycle and prevents any underlying objects from responding to the hit event.
If your button
is from the widget.*
library, you can achieve the same result more simply by disabling it and making it invisible:
button:setEnabled( false )
button.isVisible = false
By the way, the advantage of using isVisible
(rather than changing alpha
) is that you don't need to keep track of the alpha value before hiding the button. If you later do button.isVisible = true
, the ButtonWidget will have the same alpha value as before.
Upvotes: 0
Reputation: 7390
There are 2 ways that you can disable that button in your project.
1) Just create a listener to cover
as below and return true
as follows:
function coverPressed()
return true;
end
cover:addEventListener("tap",coverPressed)
2) Check if cover
exists, and then remove the listener of logo2
as:
logo2:removeEventListener("tap", hidebg)
Keep Coding............ 😃
Upvotes: 6