Forum Topic: As2 Timer.

(100 views • 2 replies)

This topic is 1 page long.

<< < > >>
None

Marsume

Reply To Post Reply & Quote

Posted at: 8/21/09 01:42 AM

Marsume LIGHT LEVEL 05

Sign-Up: 10/22/06

Posts: 148

My timer isn't stopping and I can't figure out why. It's probably something really simple, but I cant get it.

Any ideas. It traces 5 (the MaxTime var) and then every time it runs the increment function it traces timer, but timer doesn't stop at 5 like I want it to. It keeps going on forever. So im guessing the clearInterval is being used wrong on my part or something D:

Heres my code, all on the first frame

StartTimer(5)

function StartTimer(MaxTime:Number):Void {
_root.timer = 0;
trace(MaxTime)
TimerInterval = setInterval(Increment, 1000);
if (_root.timer == MaxTime) {
clearInterval(TimerInterval);
_root.timer = 0;
}
}

function Increment():Void {
_root.timer++;
trace(_root.timer);
}


None

Afro-Ninja

Reply To Post Reply & Quote

Posted at: 8/21/09 03:56 AM

Afro-Ninja EVIL LEVEL 38

Sign-Up: 03/02/02

Posts: 13,469

you need to clear the interval from within the increment function

start timer only fires once- when the timer starts. the timer is never going to be complete at the same time it starts, so the interval never gets cleared

BBS Signature

None

BUCHANKO

Reply To Post Reply & Quote

Posted at: 8/21/09 03:58 AM

BUCHANKO LIGHT LEVEL 21

Sign-Up: 09/10/06

Posts: 164

At 8/21/09 01:42 AM, Marsume wrote: My timer isn't stopping and I can't figure out why. It's probably something really simple, but I cant get it.

Any ideas. It traces 5 (the MaxTime var) and then every time it runs the increment function it traces timer, but timer doesn't stop at 5 like I want it to. It keeps going on forever. So im guessing the clearInterval is being used wrong on my part or something D:

Heres my code, all on the first frame

StartTimer(5)

function StartTimer(MaxTime:Number):Void {
_root.timer = 0;
trace(MaxTime)
TimerInterval = setInterval(Increment, 1000);
if (_root.timer == MaxTime) {
clearInterval(TimerInterval);
_root.timer = 0;
}
}

function Increment():Void {
_root.timer++;
trace(_root.timer);
}

Doubt you need help from me but meh... i'd just make avariable called " time " and duh... -- it until it goes to 0... count seconds, set the variable and urm... kinda all done =.=

Having a problem with AS 2 ( Action Script 2 ) ? || Need flash help or tips for beginners? || Are you a drawer who needs an Actionscripter to make a game?
THEN CLICK HERE !!!


All times are Eastern Standard Time (GMT -5) | Current Time: 04:38 PM

<< Back

This topic is 1 page long.

<< < > >>
You need a Grounds Gold Account to post on the NG BBS! If you don't have one, click here to sign up now! It's fast, free, and easy — and opens up tons of great NG features!