Skip to main content
Participant
October 17, 2009
Question

Put a timer on a frame | Interval

  • October 17, 2009
  • 1 reply
  • 1186 views

Hi all,

I want to make something like this:

On internet I've found this Actionscript 3.0 code:

This is literally a little script that I have found on the Internet:

this.stop(); //zodat de stage op frame 1 blijft

var timer:Timer = new Timer(3000,1);//een timer aanmaken die 1x afgaat na 30 seconden

timer.addEventListener(TimerEvent.TIMER, timerHandler); //luisteren wanneer de timer afgaat

timer.start(); //de timer starten

function timerHandler(event:TimerEvent) //wat als timer afgaat?
{
    if (this.currentFrame == 1) { //als huidig frame frame 1 is:
        this.gotoAndStop(2); //naar frame 2 gaan
    }
}

So if I am on frame 1 that it automatically after 30 seconds goes to frame 2. I have also created buttons at the bottom, so it moves from frame 1,  to 2, 3, etc. by clicking on it. How can I fix this?

Thanks

I am a novice with AS3 would like to make a similar something with 6 frames (something like the example image above)

In the above example stops the timer in frame 2 but what is the code to frame 3, 4, 5, and then start over at frame 1?
I hope somebody can help me!
This topic has been closed for replies.

1 reply

Ned Murphy
Legend
October 17, 2009

Based on the code you showed the interval will be 3 seconds, not 30.  In any case, to make the timer work continuously and move from frame to frame, try the following in its place...

this.stop();

var timer:Timer = new Timer(3000); // changed
timer.addEventListener(TimerEvent.TIMER, timerHandler);

var lastFrame:uint = ?; // replace ? with the frame number where it should go back to frame 1

function timerHandler(event:TimerEvent)
{
    if (this.currentFrame == lastFrame) {

        this.gotoAndStop(1);
    } else {

          nextFrame();

     }


}


timer.start();
Participant
October 18, 2009

Dear Ned,

Thanks for your great help!

There continues exist however a problem.  And that is that the Movieclip all frames take place to and with the last frame starts at frame 1.

Hereafter it goes wrong.

The cursor jumps then of frame 1 to 3 to 5 and then to frame 2 or five again.

How can I change this so that all frames take place up to and including the last frame and then ongoing at frame 1 starts.

Also I want gladly make buttons at each photograph so that the button on frame 1 goes also effectively to frame 1 if one clicks there on.

But what kind of code do I need for the buttons?

Looking forward to your answer and great help,

Kind regards,

Leon

Ned Murphy
Legend
October 18, 2009

This same problem came up a few days ago, and it took me awhile to recall why this happens, but the problem is fairly easy to solve...  If you have your code in frame 1, then each time you return to frame 1 you initiate a new timer, so you eventually end up with multiple timers running simultaneously.

One way around this is to only use frame 1 as the code base and have your movie operating from frame 2 onwards, such that each time it reaches the last gframe it goes back to frame 2 instead of frame 1.

Another way around this is to eliminate the timer each time you get to the last frame portion of the conditional (using timer.stop(), or setting it to only repeat as many times as needed for one set of frames).

Personally, I'd go with the first approach so that you have one timer and aren't leaving possible remnants of new instances of them all over the place.

As far as using buttons goes, you have to code buttons to do send the timeline to paricular frames.  Do you not know how to code buttons using AS3?