How to detect when touch has ended

Hi, simple problem…

If I have a display object with a touch event listener, how do I detect when a touch has moved outside of the button? The finger is still on screen, but I want to make the touch end.

thanks

Tom [import]uid: 55068 topic_id: 16283 reply_id: 316283[/import]

setFocus on that object in began … if you search the forums, this one has been answered plenty of times

cheers,

?:slight_smile: [import]uid: 3826 topic_id: 16283 reply_id: 60626[/import]

(shakes fist at Jayant’s superior answer! Anyway, my answer is just a retread of other, better ones, I’m sure…I’m pretty sure somebody told me how to do this, anyway…)

You would need to detect where the touch is.

This is a really simple example, but if your button is 100x100 at x:200, y:200, you could make a basic test like

[code]
local isWithinBounds =
– Check if inside the button along X
event.x > button.contentBounds.xMin and
event.x < button.contentBounds.xMax and

– Check if inside the button along Y
event.y > button.contentBounds.yMin and
event.y < button.contentBounds.yMax[/code]

Okay, I’m not sure linebreaks are allowed but you get the idea. From there you can use easy arguments like this:

if not isWithinBounds then -- literally, "if isWithinBounds == false, AKA you're outside the button" dothis() else dothat() end [import]uid: 41884 topic_id: 16283 reply_id: 60631[/import]

@richard,
if you use that, without the setFocus, you will get the event.target as the new object not the one you started the touch with. So you will find that it will not end.

Another things in your code, you are checking with button… that is like hardcoding, you are then going to have a very specific routine, not a generic modular code that can manage not one but several buttons.

hope that makes sense,

cheers,

?:slight_smile: [import]uid: 3826 topic_id: 16283 reply_id: 60633[/import]

local button = display.newImage( "mybuttongraphic.png" )local button:touch( event ) if event.phase == "began" then display.getCurrentStage():setFocus( self ) self.isFocus = true elseif self.isFocus then local bounds = self.contentBounds local x, y = event.x, event.y local isWithinBounds = bounds.xMin <= x and bounds.xMax >= x and bounds.yMin <= y and bounds.yMax >= y if event.phase == "moved" then if not isWithinBounds then display.getCurrentStage():setFocus( nil ) self.isFocus = false end elseif event.phase == "ended" or event.phase == "cancelled" then -- do something here display.getCurrentStage():setFocus( nil ) self.isFocus = false end end return trueendbutton:addEventListener( "touch", button )[/code] [import]uid: 52430 topic_id: 16283 reply_id: 60738[/import]