Reputation: 67
hi guys I've been trying this for hours, buts still i can't loop this function, at least i want to repeat it unto 5 times, but it only loop for once, i tried using for loop, while, and do while, but still it won't loop
var myTimer:Timer = new Timer(1000); // 1 second
myTimer.addEventListener(TimerEvent.TIMER, runMany);
myTimer.start();
function runMany(event:TimerEvent):void {
myTimer.stop();
myTimer.start();
trace("runMany() called @ " + getTimer() + " ms");
if(getTimer() > 3000)
{
//random call of captopn and costumer in 3 seconds
myTimer.stop();
capsChoice = randomRange(1, 3);//random caption
costumChoice = randomRange(1, 3);//randomcostum
//condition in caption
more codes in here………...
}
}
Upvotes: 0
Views: 1088
Reputation:
Try removing myTimer.stop() in the eventlistener function, because that function call stops your entire Timer object once it's called.
Also, when you create a Timer, you can give it a repeat counter in the Constructor Paramters.
var myTimer:Timer = new Timer(1000, 5); // 1 second, repeat 5 times
myTimer.addEventListener(TimerEvent.TIMER, runMany);
myTimer.start();
function runMany(event:TimerEvent):void {
//...
this will call the function 5 times with a delay of 1 second between executions.
And another thing to keep in mind, you only need to start the timer once, if you call myTimer.start() in the listener function every time, it will try to loops of 5 executions again and again, leading to an infinite loop.
See also : AS3 Documentation Timer
Upvotes: 1