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
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
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]