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!!! [import]uid: 95495 topic_id: 16684 reply_id: 316684[/import]
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!!! [import]uid: 95495 topic_id: 16684 reply_id: 316684[/import]
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,
?
[import]uid: 3826 topic_id: 16684 reply_id: 62360[/import]
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:
[code]
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
[/code] [import]uid: 19626 topic_id: 16684 reply_id: 62433[/import]
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!! [import]uid: 95495 topic_id: 16684 reply_id: 62458[/import]
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:
if (event.time - buttonPressStart \> 3750) and (event.time - buttonPressStart) \< 4250) then . . .
[import]uid: 19626 topic_id: 16684 reply_id: 62463[/import]
No, I mean even if they keep pressing…once it exceeds 4s, it chances the scene no matter what the user’s doing!! [import]uid: 95495 topic_id: 16684 reply_id: 62467[/import]
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.
[import]uid: 19626 topic_id: 16684 reply_id: 62502[/import]
Nice…could you give me a hand with the code? [import]uid: 95495 topic_id: 16684 reply_id: 62510[/import]
Try this:
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)
Of course, not tested, but it should get you close.
[import]uid: 19626 topic_id: 16684 reply_id: 62527[/import]
It didn’t work…hehe
But I got the idea. Terminal told me :
unexpected symbol near ‘{’ in line 4
any thoughts?
[import]uid: 95495 topic_id: 16684 reply_id: 62838[/import]
Dun, the out the {}. The old C programmer in me came out for a minute.
– 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
[/code] [import]uid: 19626 topic_id: 16684 reply_id: 62847[/import]
Hey, no luck!
Here’s my code…can you see something wrong with it?
[code]local buttonPressed = false
local buttonTimer = nil
local function triggerAction(event)
if buttonPressed then
director:buttonPressed(event.target.scene)
else
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
buttonPressed = false
timer.cancel(buttonTimer)
end
return true
end
finger:addEventListener(“touch”, buttonPressed)[/code] [import]uid: 95495 topic_id: 16684 reply_id: 62877[/import]
Oops my bad… again I didn’t test the code.
I’m using “buttonPressed” both as a variable and as a function.
lets try:
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 isButtonPressed(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", isButtonPressed)
or with your code:
[code]
local buttonPressed = false
local buttonTimer = nil
local function triggerAction(event)
if buttonPressed then
director:buttonPressed(event.target.scene)
else
end
return true
end
local function isButtonPressed(event)
if event.phase == “began” then
buttonPressed = true
buttonTimer = timer.performWithDelay(4000, triggerAction)
elseif event.phase == “ended” then
buttonPressed = false
timer.cancel(buttonTimer)
end
return true
end
finger:addEventListener(“touch”, isButtonPressed)
[/code] [import]uid: 19626 topic_id: 16684 reply_id: 62878[/import]
Hm, still not working!! heheh
I also tried changing my buttonPressed for isButtonPressed at the director code…but nothing!
Did it work there? [import]uid: 95495 topic_id: 16684 reply_id: 62881[/import]
Are you getting an error?
Can you put some prints in various places to see where you’re getting too, what is firering and when?
Can you print out the value of event.target.scene to make sure its what you think it is?
[import]uid: 19626 topic_id: 16684 reply_id: 62882[/import]
I got this:
Runtime error
…vartsman/Desktop/Hello World/Daily Fortune/daily.lua:46: attempt to index field ‘target’ (a nil value)
stack traceback:
[C]: ?
…vartsman/Desktop/Hello World/Daily Fortune/daily.lua:46: in function ‘_listener’
?: in function <?:446>
?: in function <?:215>
It refers to event.target.scene…
Maybe it is because my event listener is in the other function…it was working when I was using the previous method without the timer…what do you think?? [import]uid: 95495 topic_id: 16684 reply_id: 62883[/import]
Its most likely due to the fact that timer.performWithDelay() doesn’t pass “target” as a value in the event table. After all its just a timer, its not tied to an object like a touch or collision event would be.
Before when you were checking it, it was from a “touch” event which does pass the target in as part of the event table.
Are you going to have multiple of these buttons? Or will it always just be “finger”?
If its the latter case, just change event.target.scene to finger.scene.
If its the first case, then you’re going to need to pass a parameter to the event handler using an enclosure. I’m not very good with those.
local function triggerAction(target)
if buttonPressed then
director:buttonPressed(target.scene)
else
end
return true
end
local function isButtonPressed(event)
if event.phase == "began" then
buttonPressed = true
local myClosure = function() return(triggerAction(event.target) end
buttonTimer = timer.performWithDelay(4000, myClosure)
elseif event.phase == "ended" then
buttonPressed = false
timer.cancel(buttonTimer)
end
return true
end
[import]uid: 19626 topic_id: 16684 reply_id: 62886[/import]
I’m almost giving up…hahaha
So should I loose the event listener?
Its just the one button…I’m not sure where to put event, or target, or finger…here’s the latest version I got, still not working:
[code]
local buttonPressed = false
local buttonTimer = nil
local function triggerAction(event)
if buttonPressed then
director:isButtonPressed(finger.scene)
else
end
return true
end
local function isButtonPressed(event)
if event.phase == “began” then
buttonPressed = true
buttonTimer = timer.performWithDelay(4000, triggerAction)
elseif event.phase == “ended” then
buttonPressed = false
timer.cancel(buttonTimer)
end
return true
end[/code] [import]uid: 95495 topic_id: 16684 reply_id: 62890[/import]
did you try my last suggest with the myClosure bit?
[import]uid: 19626 topic_id: 16684 reply_id: 62911[/import]
Yeah I did…without the listener, I got no error messages whatsoever…it just doesnt do anything!
[import]uid: 95495 topic_id: 16684 reply_id: 62917[/import]