Simple button question - prevent button action when release outside button

If I press a button and then move my finger outside the button before I release, it is recognising it as a button click still. Is it possible to prevent this so that it only recognises it as a button click if the finger is released over the button?

Here is my button code which I have been using. I can’t recall where I found this code

  
local resetButton = ui.newButton{  
 default = "images/reset.png",  
 over = "images/resetHi.png",  
 onPress = button1Press,  
 onRelease = button1Release,  
 text = "",  
 emboss = true  
 }  
  
 resetButton.x = 160; resetButton.y = 276  
  
 local function clickResetButton ( event )  
 if event.phase == "ended" then  
 saveBestScore(0)  
 setBestScore(0)  
 updateOptionsHighscoreDisplay()  
  
 end  
 end  
 resetButton:addEventListener("touch",clickResetButton)  
  

Thanks

Paul [import]uid: 7863 topic_id: 4338 reply_id: 304338[/import]

edit: Sorry, I didn’t read your message very carefully. You’ll want to track the finger position whether it’s in the bounds of the button. It might be better to update a variable in the “moved” event and then do a check in the ended phase

TOUCH:
[lua]buttonlistener = function(event)
if (event.phase == “began”) then
–When you first touch the button
elseif (event.phase == “moved”) then
–When your finger moves while down
elseif (event.phase == “ended” or event.phase == “cancelled”) then
–When you lift your finger
if not (event.x > (funnyButtonOn.x - funnyButtonOn.width/2)
and event.x < (funnyButtonOn.x + funnyButtonOn.width/2)
and event.y > (funnyButtonOn.y - funnyButtonOn.height/2)
and event.y < (funnyButtonOn.y + funnyButtonOn.height/2) then
–DO ACTION
end
end
end

funnyButton:addEventListener(“touch”, buttonlistener)[/lua]
[import]uid: 11024 topic_id: 4338 reply_id: 13515[/import]

Thank you finnk

I did come up with a solution after looking at the source code of the ui.lua file.

Not sure how robust this solution is but I think it is pretty much what you are suggesting and so far it appears to have worked without any issues in the simulator. Guess I should check it on the phone before I crack open the beer

[code]

local resetButton = ui.newButton{
default = “images/reset.png”,
over = “images/resetHi.png”,
onPress = button1Press,
onRelease = button1Release,
text = “”,
emboss = true
}

resetButton.x = 160; resetButton.y = 276

local function clickResetButton ( event )

local self = event.target

if event.phase == “ended” then

local bounds = self.stageBounds
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 isWithinBounds then

saveBestScore(0)
setBestScore(0)
updateOptionsHighscoreDisplay()

end

end
end
resetButton:addEventListener(“touch”,clickResetButton)
[/code] [import]uid: 7863 topic_id: 4338 reply_id: 13517[/import]

i amended the ui file like this
http://developer.anscamobile.com/forum/2010/05/15/onrelease-not-working-when-user-release-outside-button-clicked#comment-10813

to add an onReleaseOutside event

seems to work, might not be totally right [import]uid: 6645 topic_id: 4338 reply_id: 13519[/import]

Thanks jmp909 - looks like a good solution. I will give this a go as quicker than amending all my individual buttons [import]uid: 7863 topic_id: 4338 reply_id: 13612[/import]

You might want to check out this utility:

http://www.gieson.com/corona/PlayPauseButton.zip

… which is a play pause button “class”

The playPauseButton.lua file is heavily commented, so it should help you get a grip on things.

-mike

[import]uid: 616 topic_id: 4338 reply_id: 22557[/import]