-
Problem with loop and timer CS5.5
I have a For loop set up but I want a pause every increment, so I set up the below code using a timer and a boolean variable to try and force the parser (if that's the correct word) to run through 50 ticks of the timer each increment of the loop. Problem is, while the timer is running the For loop still continues.
Code:
for(i = 0; i < 100; i++){
if (flag == true){
myTimerTest.reset(); myTimerTest.start();
}
else{
RepairWall();
flag = true;
}
}
function timerTestHandler(event:TimerEvent){
if (myTimerTest.currentCount==50){
myTimerTest.stop();
flag = false;
}
}
function RepairWall():void{
// do stuff using i variable
}
(this isn't the complete program btw, just the relevant code.)
-
.
Hi,
Does this help ?
PHP Code:
var myTimerTest:Timer = new Timer(1);
var i:Number = 0;
myTimerTest.start();
myTimerTest.addEventListener(TimerEvent.TIMER,timerTestHandler);
function timerTestHandler(event:TimerEvent)
{
trace(myTimerTest.currentCount);
if (myTimerTest.currentCount == 50)
{
//trace(i + " Finished");
RepairWall();
if ( i < 100)
{
i++;
myTimerTest.reset();
myTimerTest.start();
}
else
{
myTimerTest.stop();
myTimerTest.removeEventListener(TimerEvent.TIMER,timerTestHandler);
}
}
}
function RepairWall():void
{
trace("Timer " + i + " finished - Repaired wall " + i);
}
-
.
Hi,
I notice you said have a delay before next run through, you could always do it like this then
PHP Code:
var myTimerTest:Timer = new Timer(1);
var spinTimes:Number = 3;
var i:Number = 0;
myTimerTest.start();
myTimerTest.addEventListener(TimerEvent.TIMER,timerTestHandler);
function timerTestHandler(event:TimerEvent)
{
trace(myTimerTest.currentCount);
if (myTimerTest.currentCount == 50)
{
trace("Timer " + i + " finished");
trace("Wait 1 second for next run");
myTimerTest.stop();
myTimerTest.reset();
setTimeout(RepairWall, 1000);
}
}
function RepairWall():void
{
trace("Repaired wall " + i);
if ( i < spinTimes)
{
i++;
myTimerTest.start();
}
else
{
myTimerTest.stop();
myTimerTest.reset();
myTimerTest.removeEventListener(TimerEvent.TIMER,timerTestHandler);
}
}
-
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|