Button - minimum time to be pressed trouble

Hi!!

I need to set my button to be pressed for exactly 4 seconds before it can execute it's function. How do I do that??

Thanks!!!!!!

you can set a boolean value onBegan and a timer to trigger after 4 seconds, if when the timer triggers, the event has not ended, then you have it pressed, otherwise it has failed to pass the 4 second test. EASY

cheers,

?:)

I'd go a different route....

Make sure your button is responding to a "touch" event instead of "tap"

then in the event handler for the button:

1
2
3
4
5
6
7
8
9
10
11
12
13
buttonPressStart = 0
 
local function buttonPressed(event)
    if event.phase == "began" then
        buttonPressStart = event.time
    elseif event.phase == "ended" then
        if event.time - buttonPressStart > 4000 then -- assuming time in miiliseconds)
            -- do your activity if held for at least 4 seconds
        else
            -- do whatever if they don't hold it long enough
        end
    end
end

Thanks a lot!!!
Your code worked..
One question though: now it waits until 4s, but it just moves along if I stop pressing. Can I make it do its function exactly when it reaches 4s and not a milisecond later?

Jayant, thanks anyway, i'm sure it would have worked too!!

Well if you want the user to release exactly at 4.000 seconds, that's going to be hard. Most people's reflexes are not that good.

What you could do is test for a range of time:

1
if (event.time - buttonPressStart > 3750) and (event.time - buttonPressStart) < 4250) then . . .

No, I mean even if they keep pressing...once it exceeds 4s, it chances the scene no matter what the user's doing!!

Jay's method will fire of at 4 seconds exactly. So you might want to use it instead.

The touch event won't be fired until the person lets up on the button or movement happens, so the event method won't work if you want it to fire when it gets to 4 seconds.

Nice...could you give me a hand with the code?

Try this:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
local buttonPressed = false
local buttonTimer = nil 
 
local function triggerAction(event) {
    --
    -- We are here because the timer fired after 4 seconds
    -- if the button is still pressed, then do our biz, if not
    -- do nothing I suppose
    if buttonPressed then
        -- do what what you need to do here
    else 
        -- if you need to do anything if the button is timed out and
        -- its not pressed.  To be honest, we should not be able to get
        -- since we cancel the timer if we let up too early
    end
    return true
end
}
local function buttonPressed(event)
    if event.phase == "began" then
        buttonPressed = true
        buttonTimer = timer.performWithDelay(4000, triggerAction)
    elseif event.phase == "ended" then
        --
        -- Apparently the player let up on the button too early!
        -- Reset everything
        --
        buttonPressed = false
        timer.cancel(buttonTimer)
    end
    return true
end
 
local myButton = display.newImageRect("button.png", 64, 48)
...
myButton:addEventListener("touch", buttonPressed)
views:1345 update:2011/10/22 9:46:13
corona forums © 2003-2011