If I understand you correctly, you want to release the button when the touch moves outside its bounds? If so, then something like this will do it:
Edit: I just remembered that this routine is used for some code where I take action when the button is pressed, and no action when it is released. For “normal” button behavior, where you want to take action when the button is released while the touch is within the bounds, but cancel if the touch is moved outside the button’s bounds, then this wouldn’t work. You would want to treat the movement out of bounds as a “cancel” of the event.
[lua]local function buttonTouch(event)
local phase = event.phase
local target = event.target
local bounds = target.contentBounds
if ( “began” == phase ) then
display.getCurrentStage():setFocus(target)
target.isFocus = true
return 1 --pressed
elseif ( target.isFocus ) then
if ( “moved” == phase ) then
–If moved, and no longer within button bounds, return release.
if ( (event.x < bounds.xMin) or (event.x > bounds.xMax) or
(event.y < bounds.yMin) or (event.y > bounds.yMax) ) then
display.getCurrentStage():setFocus(nil)
target.isFocus = false
return 2 --released
else
–Moved but still inside button bounds.
return 0 --no change
end
else
–Anything else is a release.
display.getCurrentStage():setFocus(nil)
target.isFocus = false
return 2 --released
end
end
return 0 --no change
end
_[/lua]
[import]uid: 22076 topic_id: 29033 reply_id: 116844[/import]