When I touch an object, listener of the object behind it is called along with the listener of current object.
How to resolve this?
local rect=display.newRect(0,0,1000,1000); rect:setFillColor(1,0,0); local function r(event) print("rect") end rect:addEventListener("touch",r) local rect2=display.newRect(0,0,200,200) rect2:setFillColor(0,1,1) local function r2(event) print("rect2") end rect2:addEventListener("touch",r2)
local rect=display.newRect(0,0,1000,1000); rect:setFillColor(1,0,0); local widget=require"widget" local function r(event) print("rect") return true end rect:addEventListener("tap",r) local function r2() print("button") -- return true end local rect2=widget.newButton( { width = 240, height = 120, defaultFile = "start.png", overFile = "stop.png", label = "button", onRelease = r2 }
)
Console output when i clicked the button
11:58:07.475 button
11:58:07.475 rect
I changed the _return true _for both functions.Still same problem.What to do??
Your problem lies in using: rect:addEventListener(“tap”,r).
Unless I am mistaken, tap events go through all display objects in the event location. If you press and hold your button, move your mouse/finger a few pixels and then let go, then the tap event will not fire but instead the touch event activates. This is because if you just click on the button, that action is registered both as a tap and as a touch event. Tap events are sort of like touch events where the event begins and ends without entering the moving phase at any point.
If you change the tap event to a touch event and then include the “return true” as Appletreeman suggested, then it’ll work.
local rect=display.newRect(0,0,1000,1000); rect:setFillColor(1,0,0); local widget=require"widget" local function r(event) print("rect") return true end rect:addEventListener("tap",r) local function r2() print("button") -- return true end local rect2=widget.newButton( { width = 240, height = 120, defaultFile = "start.png", overFile = "stop.png", label = "button", onRelease = r2 }
)
Console output when i clicked the button
11:58:07.475 button
11:58:07.475 rect
I changed the _return true _for both functions.Still same problem.What to do??
Your problem lies in using: rect:addEventListener(“tap”,r).
Unless I am mistaken, tap events go through all display objects in the event location. If you press and hold your button, move your mouse/finger a few pixels and then let go, then the tap event will not fire but instead the touch event activates. This is because if you just click on the button, that action is registered both as a tap and as a touch event. Tap events are sort of like touch events where the event begins and ends without entering the moving phase at any point.
If you change the tap event to a touch event and then include the “return true” as Appletreeman suggested, then it’ll work.