getTimer()

Hey flashers, I’m trying to have Flash pause for five seconds before it executes a second block of code on a button. Here’s the code I got thus far:

on (press) {
_parent.gotoAndPlay(“reverse”);
}
on (release) {
pause = getTimer();
if (pause>5000) {
;
}
loadMovie (“NYHsyn.swf”, “_level0.container”);
_level0.container._x = 36.0;
_level0.container._y = 72.0;
}

Basically what I want to occur is, after a movie clip finishes a reverse a animation which ends on frame 40 of an MC, then, I want the loadmovie action to execute. The reverse animation takes at least five seconds, so I need to pause the second code of execution for five seconds. Thanks for any aid.

Manhattan

Hello Manhattan,

here is what I suggest you do:

First of all, this works best when you have a button within a movie clip. In fact this suggestion works only with this setup as it makes use of the onClipEvent() handler. Give your movie clip the instance name timer The code below should be attached to timer:


onClipEvent(load) {
  this.pause = 5000; //milliseconds
  this.init = false;
}

onClipEvent(enterFrame) {
  if(this.init == true) {
    if(!this.elapsedTime) 
      this.elapsedTime = getTimer();

    if(getTimer() - this.elapsedTime >= this.pause) {
      loadMovie ("NYHsyn.swf", "_level0.container"); 
      _level0.container._x = 36.0; 
      _level0.container._y = 72.0; 
    } 
  }
}

Now attache the following code to your button inside the movie clip:


on(release) {
  timer.init = true;
}

Let me know if you have any problems with the code.

:slight_smile:

-ptolemy

The code didn’t work. But I came up with another option:

I can run both blocks of code on the button, then, on the first frame of the loaded movie, have some script that will pause the play of the movie. In that way, my reverse animation would have time to complete without any content form the loaded movie showing.

Now, what code should I use in a frame to pause the movie for 3 seconds say?

Thanks so much.

Sorry 'bout the bogus code, I theorized a little too much I guess…

Now, what code should I use in a frame to pause the movie for 3 seconds say?

This works, I tested this time :slight_smile:


onClipEvent(load) {
  pause = 3000; 
  elapsedTime = getTimer();
  this.stop();
}

onClipEvent(enterFrame) {
  if((getTimer() - elapsedTime) >= pause) {
    this.play();
  }
}

-ptolemy

You can also try the setInterval function. You can check it in the AS Dictionnary.

pom :asian:

Yeah, I’ll use your last code for a movie clip, thanks. But listen, I’m hearing that the getTimer method only works as frames passes. In other words, here’s what I’ve heard , verbatim"

“You must use either an onClipEvent(enterFrame) or a two-frame loop, because
updating of the session timer ONLY occurs upon a frame transition. You CANNOT
use just a single frame with no frame transition to build a timer”.

My neighbor also told me something similar. Just wanted to note that for the board to see what everybody has to say about that; there may be a high-powered code or a shrewd workaround to get around that, if it is true.

Thanks people, and I’ll be around…any more pointers on the issue would be appreciated, and the board could learn more with more insight…

Thanks…

You CANNOT use just a single frame with no frame transition to build a timer

This isn’t entirely true because if you place a movie clip on the stage where both the _root and movie clip timelines each contain a single frame (i.e. no transitions), the timer I described will work. Even if you put a stop() action on the first frame of the _root level, the timer will continue to function.

To proove this, create a blank movie clip on the _root timeline and attach to it the following code:


onClipEvent(load) {
  pause = 3000; 
  elapsedTime = getTimer();
  this.stop();
}

onClipEvent(enterFrame) {
  if((getTimer() - elapsedTime) >= pause) {
    trace("time's up!");
    elapsedTime = getTimer();
  }
}

You’ll notice that the trace gets called every 3 seconds, even if you put a stop() on the first frame of _root.

On the other hand, it is true that one cannot create a timer function without makin use of a movie clip but then again, there isn’t much harm in using a blank movie clip dedicated to timing things, is there?

-ptolemy

it’s not just getTimer(), all code is executed on events. the enter frame event is just one, there are mouse events, load events, data events, select events, change events … and others i’m forgetting.

if you’re putting your code in a frame, that’s pretty much equivilant to an enter frame event, except it’s particular to that frame. so yeah, you need to enter that frame repeatedly if you want your code to execute repeatedly.

a true enter frame event will execute regardless of which frame is being entered, and enter frame events will fire repeatedly at the frame rate whether the movie is playing or not.

if you’re using mx, and you want your pause at the start of your movie, you can just check to see if getTimer() returns 3000 or more:


//in frame 1
stop();
_root.onEnterFrame = function(){
   if(getTimer() >2999){
      // play the movie
      _root.play();
      // cancel the onEnterFrame
      _root.onEnterFrame = null;
   }
}

if you want one that you can use at any time, you’ll need one function to set it up, and one to check the time. this example defines the check function within the setup function:


MovieClip.prototype.pauseMovie = function(time){
   this.stop();
   var m = this.createEmptyMovieClip("MCpause",444);
   m.time = time;
   m.start = getTimer();
   m.onEnterFrame = function(){
      if(getTimer() - this.start > this.time){
         this._parent.play();
         this.onEnterFrame = null;
         removeMovieClip(this);
      }
   }
}

/* usage  (clipToPause could be _root)
   clipToPause.pauseMovie(5000);
*/

the level of the pause movie is not important except you’ll want to be sure not to overwrite a clip you want to keep. same goes for the movie name “MCpause”, it should be unique.

Ptolemy, thanks for all the aid, and the codes. And thanks also to sbeener; I’ll use the information and codes indeed.

But let me comment on the issue: although I was told by friends and others that getTimer is only workable on frame transitions, I was in doubt, knowing flash’s capabilities. That’s why I ferret out all the information on an issue, because some coders are amazing with what they come up with.

Thanks again, and I’ll be back…take care…

Hello all,

Sorry to “piggyback” this thread but a question came up while incorportating this beautiful timer. How can I pause the pause? :-\

Let me explain. I have a news section that’s grabbing my news via XML. I have used this timer to pause before grabbing the next item. I would like to delay the pause or stop the loop if the user wants to. For example if there are in the middle of reading an item, I want them to control pause the news from changing.

Does that make any sense?

I hope so…

I guess you could reset the timer with a mouse click, or button click or a mouse over…

Yeah, that’s what I had in mind… Just not sure how to rest it on any kind of action. I was planning on it being a simple button.

sure it makes sense.

one way would be to store the amount of time left on the timer, and cancel the onEnterFrame event. by storing it in MCpause, we avoid polluting the paused clip any further.


MovieClip.prototype.pauseThePause = function(){
   var p = this.MCpause;
   p.progress = p.time - (getTimer() - p.start);
   p.onEnterFrame = null;
}

then to restart just run pause again using this.MCpause.progress as the delay.


PausedClip.pauseMovie(PausedClip.MCpause.progress);