2 Replies Latest reply on Mar 18, 2010 5:32 PM by Thali-Hoo

    Stoping a timeline for set durations at various frames (AS3) ?

    Thali-Hoo Level 1

      Hi Guys,


      I'm wondering how to stop the root timeline at a given frame using action script 3 in flash CS4


      With AS2 I used this kind of script that worked all the time.


      this.createEmptyMovieClip ("timer",50);
      timer.onEnterFrame = function () {
          if (this.startTime > 0) {
              var difference = getTimer () - this.startTime;
              if (difference > this.timerLength) {
                  this.target.play ();
                  this.startTime = 0;
      function pauseFor (theTime) {
          stop ();
          timer.timerLength = theTime;
          timer.startTime = getTimer ();
          timer.target = this;


      And I called the pause duration from the frame that needed it, etc.


      Any ideas for AS3? (I've been away from flash for a year and it shows...ouch! but I'm a n00b to as3)


      Thanks in advance!!

        • 1. Re: Stoping a timeline for set durations at various frames (AS3) ?
          DigitalArchitectCanada Level 3

          Hi Thali-Hoo,


          What you're going to want to do is take advantage of the timer class here, use a document class for your FLA and use an undocumented method addFrameScript. I've written up a sample of how to do all of this and posted it in a zip file. Basically here's what I've done though:


              import flash.display.MovieClip;
              import flash.events.Event;
              import flash.events.TimerEvent;
              import flash.utils.Timer;
               * ...
               * @author Jesse Nicholson
              public class PauseTimeline extends MovieClip
                  public function PauseTimeline()
                      if (stage) {
                      }else {
                          addEventListener(Event.ADDED_TO_STAGE, init);
                  private function init(e:Event = null):void
                      addFrameScript(30, frame30);
                  private function frame30():void
                      //The playhead has just hit frame thirty. This is the same as having a script on a frame icon in the IDE
                      trace("Hello, frame thirty");
                      var delayTimer:Timer = new Timer(5000, 0);
                      //It's not the best thing to throw a function right into the handler reference but in this case it's short and sweet. See below on how you'd write the function out proper
                      //This is how you would do the listener normally
                      //delayTimer.addEventListener(TimerEvent.TIMER, frameThirtySleepTimerExpired);
                      delayTimer.addEventListener(TimerEvent.TIMER, function(TimerEvent):void { gotoAndPlay(1); } );
                  private function frameThirtySleepTimerExpired(e:TimerEvent):void
                      //Check and see if the timer listener is on the currentTarget (the delayTimer object) and remove it if exists
                      if(e.currentTarget.hasEventListener(TimerEvent.TIMER) == true){
                          e.currentTarget.removeEventListener(TimerEvent.TIMER, frameThirtySleepTimerExpired);
                      //Do whatever you want to do in here (when your timer has expired/completed)





          So, to explain. The FLA has 50 frames on the root timeline. On init (which is when the stage is present, which means that the class has fully initialize and the constructor has been processed etc blah blah) I use addFrameScript and attach the method frame30 to frame 30 on the root timeline. This is the same as just going to frame 30 in the IDE, making a keyframe and writing on the timeline.


          So when the play head lands on frame 30, it calls the frame30 function. Inside that function we stop the playing of the timeline, create a timer object with a delay of 5 seconds and instead of passing in a reference to a handler function I just write a function on a single line right in there. Not the best way but for this example it's short and clean, but I still included a sample of how to do it normally commented out.


          When the timer is started, the clock starts ticking. When the timer has finished counting through it's delay, it will fire an event to the listener handler function which only has the code "gotoAndPlay(1)" to restart playing from frame 1, looping the whole process. I think this is what you're looking for but if not or if you have any questions let me know. In my job I try to work with AS3 as much as I can but sometimes (like today) they force me to go back to AS2 and.. well basically I can sympathize with you so let me know if you need any other help transitioning to AS3! lol Hope this works for ya,




          PS - DL The example here! http://ascensionsystems.ca/Pause Timeline.zip

          1 person found this helpful
          • 2. Re: Stoping a timeline for set durations at various frames (AS3) ?
            Thali-Hoo Level 1

            Hi Jesse, and thanks for your time to reply! Greatly appreciated!


            Yeah, I've been given more jobs as lead designer and needed to use illustrator 70% of the time (photoshop the rest)...argh!!!


            OK, I tried your FLA file and added a green rectangle to the stage. But it just flashes by. Doesn't stop for the 5 seconds for some reason.


            Also with my old script, I could add the command at any frame that needed to be paused, how can I do that in AS3? (I'm 99% lost for the moment...but I am reading up on it)


            Say i have a numbered sequence from 1 to 5......where each number had a duration for an equal amount of seconds. 1=1 second, 2=2 seconds, etc, how could I do something like that....where I can easily edit the time in ms when needed, etc.


            Thanks so much for your reply!


            - Jeff