Click outside an object

I have for example three buttons, and I will check if there is a click outside the buttons, how can I solve this?

Best regards

Andreas

What is the actual problem?

You can put a touch/tap listener on your background.

Rob

Yeah, I tried this, with the background. The problem is, every tap on the background is not on the button. The second is, if I make an rect background and set the alpha to zero, no tap is propagated, or am I wrong?

I try this tomorrow again, my wife wants me to go shopping with her now…

So have fun, I will have fun too… :o

If you have an invisible object, you can use its .isHitTestable member and set it to true and it will react to touch/tap events.

Are you making your own buttons or using widget.newButton(). If you’re making your own, remember touch events have multiple phases (touch began, touch ended, potential touch moved), touch and tap are different events, and if you’re handling your own, you need to return true at the end of the handler function to make sure the touch doesn’t propagate their way through the display hierarchy.  

If you want to ensure that touches generated by an object are retained by that object until the touch is released, you can try using a touch listener like this:

local function touch(e) if (e.phase == "began") then e.target.hasFocus = true display.currentStage:setFocus(e.target) e.target.touchjoint = physics.newJoint( "touch", e.target, e.x, e.y ) -- physics API return true elseif (e.target.hasFocus) then e.target.touchjoint:setTarget( e.x, e.y ) -- physics API if (e.phase == "moved") then else e.target.hasFocus = nil display.currentStage:setFocus(nil) e.target.touchjoint:removeSelf() -- physics API e.target.touchjoint = nil -- physics API end return true end return false end

Remove the physics API lines as needed.

Thank you, that helped me much.

What is the actual problem?

You can put a touch/tap listener on your background.

Rob

Yeah, I tried this, with the background. The problem is, every tap on the background is not on the button. The second is, if I make an rect background and set the alpha to zero, no tap is propagated, or am I wrong?

I try this tomorrow again, my wife wants me to go shopping with her now…

So have fun, I will have fun too… :o

If you have an invisible object, you can use its .isHitTestable member and set it to true and it will react to touch/tap events.

Are you making your own buttons or using widget.newButton(). If you’re making your own, remember touch events have multiple phases (touch began, touch ended, potential touch moved), touch and tap are different events, and if you’re handling your own, you need to return true at the end of the handler function to make sure the touch doesn’t propagate their way through the display hierarchy.  

If you want to ensure that touches generated by an object are retained by that object until the touch is released, you can try using a touch listener like this:

local function touch(e) if (e.phase == "began") then e.target.hasFocus = true display.currentStage:setFocus(e.target) e.target.touchjoint = physics.newJoint( "touch", e.target, e.x, e.y ) -- physics API return true elseif (e.target.hasFocus) then e.target.touchjoint:setTarget( e.x, e.y ) -- physics API if (e.phase == "moved") then else e.target.hasFocus = nil display.currentStage:setFocus(nil) e.target.touchjoint:removeSelf() -- physics API e.target.touchjoint = nil -- physics API end return true end return false end

Remove the physics API lines as needed.

Thank you, that helped me much.