This rather unserious demonstration shows a seriously useful concept: the ability to use ActionScript to read time references using the getTimer() method.
The method is used here for several purposes: to control various facial features (blinks, eye position, mouth shape) and to run a 30-second countdown clock. (Reload this page to watch the fun again.)
Time-control effects were also used in the Timeline-vs-Scripting demo.
Two source files for the example above are available on Crow. The first, faceTimerDemo1.fla, contains only the script for the eyeblinks. The second, faceTimerDemo2.fla, has all the features you see above. Both are in MMShare/timer.
More About getTimer()
The statement getTimer() (actually a method) returns a value in milliseconds indicating the amount of time the current movie has been active. While that may seem a rather esoteric concept, you can also think about getTimer() in this way--every time you execute the statement, you check a digital stopwatch.
You can easily use getTimer() to establish delays and durations. Here's an illustration:
| Drag the red ball over the green or vice versa. With no time constraint on the sound you'll probably here repetition or an apparent echo. | Try dragging the balls again. This time the sound can only play if a 1.5 second delay has elapsed. Since that's about the length of the sound, we're spared the echo effects. |
Here are the lines that cause the sound to play:
if(this.hitTest(_root.redBall)){
excuseMe.start();
}
To circumvent the sound if it is already playing, we use getTimer() in two places:
if(this.hitTest(_root.redBall) && getTimer() > excuseMeTime){
excuseMe.start();
excuseMeTime = getTimer()+ 1500;
}
The first occurrence, in an additional clause within the IF condition, tests the current clock reading against a value stored in a variable called excuseMeTime. If the sound has not been played yet, this variable has no value at all, but that's not a problem. In that case, the current time value is definitely greater, so the clause evaluates as true. If we're not in a wait state, then we play the "excuse me" sound. We also take another reading from the stopwatch and add 1500 milliseconds (1.5) seconds, providing a value for excuseMeTime which will be read the next time the movie wants to play the sound.
The source files for these two examples, goodExcuses.fla and badExcuses.fla, are available on Crow in MMShare/timer.
