How to detect if button is not pressed after some time?

Hi guys,

I have small problem with events and timers. I am using corona’s ui.lua library for buttons and I can’t detect if a button is NOT pressed after some time.

Function will return true if I touch a button and false otherwise within the time limit of 3 seconds. [import]uid: 65687 topic_id: 21462 reply_id: 321462[/import]

Maybe one solution is to create listener?

function button:touch( event )  
 local count = event.count  
 local phase = event.phase  
 if ("began" == phase) and (count \>= 3) then return true  
end  

and set listener with

local a = button:addEventListener( "touch", button ) ?
[import]uid: 65687 topic_id: 21462 reply_id: 84963[/import]

Maybe using some local variables to synchronize? Anyone? [import]uid: 65687 topic_id: 21462 reply_id: 85265[/import]

I’m confused about what you are asking.

Are you trying to set up a timer which checks after 3 seconds and has two possible outcomes:

A, the button is pressed, so it returns true.

B, the button is not pressed, so it returns false?

Or are you trying to do something else? [import]uid: 67933 topic_id: 21462 reply_id: 85267[/import]

I want the function that checks if the button is pressed(at least once) in the time interval of 3seconds.

A, the button is pressed - return true - if pressed within 3seconds

B, the button is not pressed - return false.
Sorry for confusion. [import]uid: 65687 topic_id: 21462 reply_id: 85276[/import]

maybe this will help:

[code]
local isTimeOut = false

local function touchMe ( event )
if isTimeOut then
youTouchedMeTooLate()
else
wowThatWasAFastTouch ()
end
return true
end
params = { … whatever …,
onRelease = touchMe }

local myButton = ui.newButton ( params )
local myTimer = timer.performWithDelay ( 3000,
function ( event )
isTimeOut = true
end
)
[/code] [import]uid: 80100 topic_id: 21462 reply_id: 85278[/import]

ooops didn’t see your last remark.
so, according to this, the code needs to be revised:

local function touchMe ( event )  
 return isTimeOut  
end  

but I would strongly suggest you use the code snippet above which calls two different functions depending on whether the 3 seconds have expired or not.
actually this “return isTimeOut” might mess up the event bubbling, so I wouldn’t suggest returning anything than “true” inside of touch event functions (unless you want the elements behind the button to respond to touch events too ) [import]uid: 80100 topic_id: 21462 reply_id: 85289[/import]

I’m a bit busy now, but will try and work up a solution for you as soon as I can, but that may not be until tomorrow.

In the meantime and off the top of my head, take a look at timer.perform with delay.

http://developer.anscamobile.com/reference/index/timerperformwithdelay

Off the top of my head: In the event.phase == “began” you want to set a flag to true, (e.g. hasBeenPressed = true)

Then using a timer.performWithDelay call a function after 300 milliseconds which checks if hasBeenPressed = true.

[code]
function buttonPressed()
if hasBeenPressed == “true” then
–your logic here
end
end

Hope that makes sense?

Spider [import]uid: 67933 topic_id: 21462 reply_id: 85295[/import]

Ha, just realised nosheet beat me too it…
[import]uid: 67933 topic_id: 21462 reply_id: 85296[/import]

@spider_newgent @nosheet thanks guys, you were very helpful it’s all clear now.

[import]uid: 65687 topic_id: 21462 reply_id: 85300[/import]