Timer issues

Ok so im going to try and explain this problem as best as I can because its going to be the death of me. I have a function that creates a certain number of timers based on how many times the user touches a button(pressCount1). The delay of the timers is based on when the user touches the button, my issue is say the user touches the button 15 times. When the function is called, it creates 15 timers all set with increasing delays. But I want to be able to cancel all of these timers at any point. How can I do this??

function tracks1()
for i = 1 , pressCount1 do
trackTimer = timer.performWithDelay(pressTimes1[i], play1)
end
end

Let me show an example. Consider a button that plays a sound clip(BEEP!). The user presses it 5 times.
----- The # of times a user pressed the button
pressCount1 = 5
----- this holds all the times (in seconds) at which the button was pressed.(first press is always 0)
pressTimes{}= "0, 1.53, 2.34, 5.64, 8.99"

So when I run the function this is what is created:
trackTimer = timer.performWithDelay(0, play1)
trackTimer = timer.performWithDelay(1.53, play1)
trackTimer = timer.performWithDelay(2.34, play1)
trackTimer = timer.performWithDelay(5.64, play1)
trackTimer = timer.performWithDelay(8.99, play1)

----play1 just plays the sound from the button, so effectively this creates a sort of recording effect. Mimicking back the users pattern of touches.
My problem is if these timers are invoked, and then say after 2 seconds (the first two timers have already fired) and at this point I want to STOP all of the rest( the ones that fire at 2.34, 5.64, and 8.99), How can I do this?
I tried timer.cancel(trackTimer) but that will only stop the last one that I created (trackTimer = timer.performWithDelay(8.99, play1)).

Geez I hope this isn't as confusing as it seems. This has kept me stagnent on my project for a long time. Is there a way to maybe put all of those timers into a group, and then just remove the group or something?

why do you not call the next one at the end/onCompletion of the first? that way you can have synchronisation without the issues.

cheers,

?:)

You might consider a few other solutions as well.

All of these timers call the same play1() function. So why not have the play1() function test for a cancel value. So if you want to cancel all of your timers, you simply have a Boolean value like isTimerCancelled = true. Then in the play1() function that is triggered, have the first line of the function test that value... and if "isTimerCancelled==true" then just return without doing anything else in the triggered function.

If you are up to the task, however, I would create a more sophisticated Timer class and a TimerManager class. The timers are just data objects that contain when a timer is started, when it should fire, and the function it should trigger. Then the TimerManager class just tests this on every "EnterFrame" event to see if one of it's timers should be triggered. This way, you can pause, resume, or cancel any number of timers and get very sophisticated control over your timers.

I actually did this and it works great. (I am not able to share this code, but follow the logic on it, and you can write your own without much trouble.)

views:1625 update:2011/9/26 15:43:22
corona forums © 2003-2011