Button - minimum time to be pressed trouble

It still needs the listener…

[import]uid: 19626 topic_id: 16684 reply_id: 62918[/import]

Hehe I even tried with two listeners, one for each…I tried pretty much everything!! [import]uid: 95495 topic_id: 16684 reply_id: 62992[/import]

gustavoschv,

two things, generally it does not take any issue so long to get resolved. So,

  1. What exactly are you trying to do, I mean if you could have done that in code, then you would not have this thread going on for so long

  2. You are doing something with director, why do you not just start a new project, place a rectangle and see if you can have this 4 second test, there could be something else that could be interfering with your code

cheers,

?:slight_smile: [import]uid: 3826 topic_id: 16684 reply_id: 62996[/import]

Can you package up your project and send it to me. There may be something else outside of this block of code that’s interfering with us.

rob at omnigeekmedia dot com [import]uid: 19626 topic_id: 16684 reply_id: 63006[/import]

Hey Jay,
Sorry about the trouble.
My thing is: I need a button to perform it’s function exactly after it’s been pressed for 4s, non-stop.
If the user let go before 4s, it goes back to square one. But, if he keeps pressing for more than 4s, it wouldn’t matter, since my function is to change the scene. So, every other function from the scene we were in should stop, and the new scene should come up.

I got the timer working with prints at the terminal. My only question is with director. I need to change scenes, and I’m using director. But now the code for changing it

director:isButtonPressed(event.target.scene)

is in triggerAction function, so I’m not sure how to make it work from here. Just this last part.

My code now is like this:

[code]
local buttonPressed = false
local buttonTimer =nil

local function triggerAction(event)

if buttonPressed then
print ("!")

director:isButtonPressed(finger.scene)
else

end
return true
end

local function isButtonPressed(event)
if event.phase == “began” then
buttonPressed = true

local myClosure = function() return(triggerAction(event.finger)) end
buttonTimer = timer.performWithDelay(4000, triggerAction)
elseif event.phase == “ended” then

buttonPressed = false
timer.cancel(buttonTimer)

end
return true
end
finger:addEventListener(“touch”, isButtonPressed)[/code]

Then, my terminal error goes like:
Runtime error
…vartsman/Desktop/Hello World/Daily Fortune/daily.lua:47: attempt to call method ‘isButtonPressed’ (a nil value)
stack traceback:
[C]: in function ‘isButtonPressed’
…vartsman/Desktop/Hello World/Daily Fortune/daily.lua:47: in function ‘_listener’
?: in function <?:446>
?: in function <?:215>

but it still prints my (!).

Rob is being really helpful, but without testing, we’re just shooting blanks here. Lol.

Any help is appreciated…otherwise i’ll just drop it and case closed!

Thanks

[import]uid: 95495 topic_id: 16684 reply_id: 63016[/import]

ok,
so here’s the logic

if buttonPressBegin then
set flag to true
set a timer for 4 seconds
elseif buttonPressEnded then
setFlag to false
end

in the timer
if flag is false then
do nothing
else
director changeScene
end

now as I see your code you are mixing it up a bit too much.

keep it simple & sweet

 local flag = false  
 local timerHandle = nil  
  
 local button = display.newRect(10,10,100,40)  
 button:setFillColor(255,0,0)  
   
 local function onTimer(event)  
 print("The button was pressed for 4 seconds")  
 --do whatever you want here  
 --director:changeScene(.....)  
 end  
  
 local function onTouch(event)  
 local phase = event.phase  
 if "began" == phase then  
 flag = true  
 timerHandle = timer.performWithDelay(4000,onTimer)  
 else "ended" == phase then  
 flag = false  
 timer.cancel(timerHandle)  
 timerHandle = nil  
 end  
 end  
  
 button:addEventListener("touch",onTouch)  

cheers,

?:slight_smile: [import]uid: 3826 topic_id: 16684 reply_id: 63023[/import]

That’s exactly where my doubt lies!!

You cleaned up my fucked up code, that’s true (lol), but I need to know what should I put here:

–director:changeScene(…)

because, whatever I type, I get this:

The button was pressed for 4 seconds
Runtime error
daily.lua:48: attempt to index field ‘target’ (a nil value)
stack traceback:
[C]: ?
daily.lua:48: in function ‘_listener’
?: in function <?:446>
?: in function <?:215>
[import]uid: 95495 topic_id: 16684 reply_id: 63027[/import]

The error states that your code is in a file called daily.lua (obviously called from Director) and on Line 48, which is the code I posted above, you are trying to use the target value, which is giving you an error

You are *trying* to do something that you are not posting, the error would mean nothing as there is no code to relate it to.

Line 48, where the error is, is not co-relatable as most of the code posted here are about 20-30 lines only. So you need to post what you are doing for anyone to be able to help you debug.

If you use this as is, without *TRYING* your modification, it works perfectly fine. So there is something suspect with your code.

So first, Good Progamming practices, let us finish the event
so add the lines just after the print(“The button…”) statement

 local event = {}  
 event.phase = "ended"  
 button:dispatchEvent( event )  

then use the changeScene method

cheers,

?:slight_smile: [import]uid: 3826 topic_id: 16684 reply_id: 63030[/import]

The problem is because “target” doesn’t exist with a timer event. The event structure you get is different based on the type of event. Timer’s are not tied to objects and therefore DO NOT have a target to use.

Why not simply do:

director:changeScene(“thenameofthesceneyouwanttogoto”)

Hard code it. You said at one point, you only had the one button, so this will work.
If you get to where you have multiple buttons, simply set a variable when you detect the start of the press that contains a string to the scene for that particular button. [import]uid: 19626 topic_id: 16684 reply_id: 63031[/import]

So, here’s what I got…still not working!! (my button is called “finger” and the scene I need to go to is called “final”)

 local flag = false  
 local timerHandle = nil  
   
  
 local function onTimer(event)  
 if flag then  
 print("The button was pressed for 4 seconds")  
 local event = {}  
 event.phase = "ended"  
 finger:dispatchEvent( event )  
 director:chanceScene("final")  
 else  
 end  
 end  
   
 local function onTouch(event)  
 local phase = event.phase  
 if ( phase == "began")then  
 flag = true  
 timerHandle = timer.performWithDelay(4000,onTimer)  
 elseif (phase == "ended") then  
 flag = false  
 timer.cancel(timerHandle)  
 timerHandle = nil  
 end  
 end  
   
 finger:addEventListener("touch",onTouch)  

I’m getting the same error from terminal…
[import]uid: 95495 topic_id: 16684 reply_id: 63038[/import]

 local flag = false  
 local timerHandle = nil  
   
   
 local function onTimer(event)  
 if flag then  
 print("The button was pressed for 4 seconds")  
 director:changeScene("final")  
 end  
 end  
   
 local function onTouch(event)  
 local phase = event.phase  
 if ( phase == "began")then  
 flag = true  
 timerHandle = timer.performWithDelay(4000,onTimer)  
 elseif (phase == "ended") then  
 flag = false  
 timer.cancel(timerHandle)  
 timerHandle = nil  
 end  
 end  
   
 finger:addEventListener("touch",onTouch)  

I’m not sure what that dispatch code is all about. But there is also a typo in the director call. Its “change” not "chance"Scene.

[import]uid: 19626 topic_id: 16684 reply_id: 63039[/import]

Guys, it worked…thanks a lot!!!
I have other problems still, but I’ll manage them!
Thanks for the patience, I’m kinda new in the business lol
[import]uid: 95495 topic_id: 16684 reply_id: 63041[/import]