PDA

View Full Version : Timed Loop



John Keel
February 21st, 2009, 11:46 PM
I am trying to construct a timed loop... so far without success.

What I need is a loop where iteration 0 + i + 1 starts a few seconds after iteration 0 + i. I tried putting a timer inside the loop but the loop seems to simple skip the timer instead of executing it sequentially before starting the next iteration. Here is the code snippet:



var iTimer : Timer;

for( var i:int = 0; i < 2; i++ )
{
trace( i + " i iterations" );

iTimer = new Timer( 1000 );
iTimer.addEventListener( TimerEvent.TIMER, iLoop );
iTimer.start( );

function jLoop( evt:TimerEvent ):void
{
trace( i + " i iterations" );

iTimer.removeEventListener( TimerEvent.TIMER, jLoop );
iTimer.reset( );
}
}


The loop does not seem to wait for jLoop() to be executed. It speeds ahead onto the next iteration, instantiating a new timer and thus screwing up the timing of the iterations. I have tried different things like putting break and continue statements in there but to no avail. Anyone got an idea?

John Keel
February 23rd, 2009, 02:12 AM
I would have thought this would be an easy feat for all the AS3 champs around here...

dandylion13
February 23rd, 2009, 02:43 AM
Hello :)

This is interesting, but honestly, I'm a very confused about what you are trying accomplish. Maybe you can tell us what need to do and we can suggest a simpler strategy?

John Keel
February 23rd, 2009, 03:14 AM
Frankly, I have no specific objective. I did this to test something that I have since fixed. I still think it might be something interesting to know how to do though. Let's say you wanted to lay out a deck of cards or maybe tiles (adding them to the stage by looping through an array or xml list with references, for example) but you'd want them to not all appear immediately but staggered in 1/30 seconds intervals. That's one thing a timed loop would come in handy for that I can think of.

e_owen
February 23rd, 2009, 05:05 AM
import flash.utils.Timer;
import flash.events.TimerEvent;

var iTimer:Timer = new Timer(1000, 30); // loop 30 times at 1 second intervals
iTimer.addEventListener(TimerEvent.TIMER, iTimer_timerHandler);

function iTimer_timerHandler(event:TimerEvent):void {

trace (event.currentTarget.currentCount); // how many times the timer has fired
}

iTimer.start();

dandylion13
February 23rd, 2009, 05:18 AM
Perhaps you could use something like this?



_timer = new Timer(50, 52);
_timer.addEventListener(TimerEvent.TIMER, onTimer);
_timer.start();

private function onTimer(event:TimerEvent):void
{
drawCard(_timer.currentCount);
}

e_owen
February 23rd, 2009, 05:24 AM
...pretty much like I said, no?

John Keel
February 23rd, 2009, 12:48 PM
Thanks. I was actually gonna post something like this today because it occured to me last night that one could just skip the whole loop business and have the timer itself take over the job. So yeah, pretty much what you guys posted. One could set a constant for the number of timer executions and a counter for better readability but that's just cosmetics.



private const COUNT_MAX : uint = 52; // set this to however many times the timer should execute

private var mCurrentCount : uint = 0;

private var mCardArr : Array = new Array( );

private var mTimer = new Timer( 500, COUNT_MAX );
private var mTimer.addEventListener( TimerEvent.TIMER, onTimerEvt );
private var mTimer.start( );

private function onTimer( evt:onTimerEvt ):void
{
drawCard( mCardArray[ mCurrentCount ] );
mCurrentCount++;
}


If anyone knows a way to actually pause or time a loop, I'd still be interested in that just for the sake of killing the cat.

e_owen
February 23rd, 2009, 12:51 PM
Again, Im not sure why you would want to exactly but Im assuming there is a decent reason... Timer has a few methods, stop(), start() and reset() being some of them that would allow you to control the running of the loop.