Tutorial details:
Written by: Jesse Stratford
Time: 20 minutes
Difficulty Level: Intermediate
Requirements: Flash 4 or 5, (Flash 4 code will be significantly different).
Topics Covered: How to perform various events every X seconds.
Assumed knowledge: Variables, Paths, MovieClip Actions, Conditional Statements, basic math.

This face will duplicate at a random point, every two seconds, until there are 20 faces, then stop.
Download the source here.

The inspiration behind this tutorial is two fold. Firstly, it's generally useful to know how to do, and secondly it will stop us having to explain how every time someone wants to perform such an action. Recently there's been a lot of requests along the lines of "I want to do <some event> every <random number of> second until <some condition is met>". Now in Flash 4 most people achieved this using very long MovieClips with lots of empty frames, or the more sophisticated used GetTimer. In Flash 5 we have all sorts of fancy controls over the time elements and I still prefer the getTimer method. Call me old fashioned if you will.

Let's get into it! There's not really much of a need for an intro to this tutorial so I'll just show you the code and explain how it works, shall I?

Define this variable in Frame 1 of your movie:

_root.wait = 10;

This 'wait' variable is the number of second to wait between each event. It must be an integer greater than 1! For those of you who don't speak Math, that means a whole number, so no decimal points or fractions.

Create a controller MC (just a blank MC which will sit off the stage somewhere) and add these actions:

onClipEvent (enterFrame) {
 if (_root.delay) {
  if (Math.floor(getTimer()/1000) == _root.lastTime+1) {
   _root.delay = false;
 } else if (Math.floor(getTimer()/1000) == _root.lastTime+_root.wait) {
  _root.lastTime = Math.floor(getTimer()/1000);
  _root.delay = true;
  // custom actions go here

What it does (line by line):

  1. Perform these actions every frame. So if your frame rate is 12fps, these actions will be performed 12 times per second while the movie is playing.
  2. If the variable delay on the _root level is True, then;
  3. If the current time in seconds is one second greater than the last time logged, where;
    • Math.floor() - Rounds the number in the brackets down to the nearest whole number.
    • getTimer() - The time in thousandths (1/1000) of seconds since the movie began playing. Hence getTimer()/1000 is the time in whole seconds since the movie began playing.
    • _root.lastTime - A root level variable which stores the time at which we last executed out events. On the first run-through this code, the events have not been executed at all so this variable is equal to 0.
  4. Set the variable delay on the _root level to False.
  5. End If
  6. (Else clause for Line 1's If statement) - If the current time in seconds is equal to the last time logged plus the time interval to wait, where;
    • Math.floor() - As above, see 3.i.
    • getTimer()/1000 - As above, see 3.ii.
    • _root.lastTime - As above, see 3.iii.
    • _root.wait - A root level variable which stores the time interval to wait between actions.
  7. Set the variable lastTime on the root level to the current time.
  8. Set the variable delay on the root level to True.
  9. Comment. This is where you will insert your own scripts which perform the actions you wish to perform every X seconds.
  10. End If
  11. End onClipEvent.

Don't worry, I explain it a bit better over the page ...