Skip to main content
Participating Frequently
June 22, 2009
Question

I need help with Timers

  • June 22, 2009
  • 2 replies
  • 937 views

Hi all.. I posted a retardedly long problem last time and apologize for that. Now I am going to break the whole problem into little pieces. This post is regarding timers.  I have 3 different banner images that have an "out" animation based on the timeline. I am trying to add a delay at specific points on the timeline. Here is my code for the first frame:

//TIMER
var moveFrame:int = currentFrame + 1;
stop();
var millis:Number=5000;
var Tim:Timer = new Timer(millis, 1);
Tim.addEventListener(TimerEvent.TIMER, playit);

function playit(e:TimerEvent):void {
gotoAndPlay(moveFrame);

Tim.start();

So this does exactly what i want at first... it waits 5 seconds, then it calls for the integer of moveframe which is currentFrame + 1. This then moves properly through the "out" animation and to the next banner which has this code:

stop();
Tim.start();
trace (moveFrame);

The problem is that the var Tim does not seem to follow the currentFrame + 1 idea (or more likely, I am not properly writing this script) in my head. I want to be able to reuse the timer script from frame one but i want it to add 1 to the current frame that I am on and play (say frame 10 is the second banner with the second portion of code I put down, I would want it to go to frame 11 utilizing the script from frame 1). Unfortunately, all it is doing is going back to frame 1 then adding 1 which is why my trace output is always 2 haha!  Is there a way to reuse this type of timer code without having to call frame labels? I just want a true currentFrame + 1 solution. Thanks for your time.

This topic has been closed for replies.

2 replies

Ned Murphy
Legend
June 22, 2009

When you define the var moveFrame in frame 1, that's a one shot deal.  It does not automatically adjust just because you use a variable in setting its value.  You need to reassign the moveFrame value if you want it to change.  So when you get to frame 10, you either assign moveFrame to currentFrame + 1 again or forget about using it at all, and just use currentFrame+1 all the time.

Participating Frequently
June 22, 2009

Thank you both, I will take your advice and work on it tonight. I think I can get it worked out now thanks!

Participating Frequently
June 28, 2009

First off, I wanted to thank you guys for helping me, I got the timers to work properly.  Now I am having trouble pausing the timers on a mouse over event.

Basically each timer represents a static position for a banner, when the timer triggers the playhead moves to the next timer and a new banner shows up.  I want to pause the timer when someone rolls over the banner. Here is what I tried but it failed miserably:

//Here is the timer, it removes itself once it triggers

stop();
var Tim:Timer = new Timer(5000, 1);
Tim.addEventListener(TimerEvent.TIMER, playit);
Tim.start();

function playit(e:TimerEvent):void {
gotoAndPlay(currentFrame +1);
}
Tim.addEventListener(TimerEvent.TIMER_COMPLETE, timerDone1);
function timerDone1(e:TimerEvent):void{
Tim.removeEventListener(TimerEvent.TIMER, playit);
}

//Banner roll over function

banner1_mc.addEventListener(MouseEvent.ROLL_OVER, pauseTim);
function pauseTim(event:MouseEvent):void {
    Tim.stop();
}

banner1_mc.addEventListener(MouseEvent.ROLL_OUT, resumeTim);
function pauseTim(event:MouseEvent):void {
    Tim.start();
}

There doesn't seem to be a working "pause function", all that works is a stop function but it resets the timer to 0.

Participating Frequently
June 22, 2009

k, i just have sugegstions based on experience.

Always kill timers after use, even when u are going to use them again.

in ur case i think u need to use TimerEvent.TIMER_COMPLETE , because u need it only one time right?

so once timer is complete, u have to remove remove listhener , for the new one u can create it again.

use the same instance of timer.

Also for the jumping frame , u have to update ur variable on next banner: moveFrame= currentFrame + 1;

hope that helps