7.4. Reversing Playback
Problem
You want to play a movie clip’s timeline in reverse.
Solution
Use an onEnterFrame( ) event
handler
with the prevFrame( )
method.
Discussion
You can programmatically reverse the playback of a movie
clip’s timeline with ActionScript. The
onEnterFrame( ) event handler of a movie clip is
called repeatedly at the same frequency as the
movie’s frame rate. Therefore, if you create an
onEnterFrame( ) method definition for a movie
clip that includes a call to the prevFrame( )
method, the movie clip’s timeline will continually
move to the previous frame at the movie’s frame
rate:
myMovieClip.onEnterFrame = function ( ) {
this.prevFrame( );
};If the movie clip’s playhead is already on the first
frame, prevFrame( ) has no effect. Therefore, to
loop the movie clip’s timeline while playing in
reverse, add a conditional statement that goes to the last frame of
the movie clip if it is already on the first frame. The current frame
number is determined using the _currentframe
property, and the last frame is determined using the
_totalframes property:
myMovieClip.onEnterFrame = function ( ) {
if (this._currentframe > 1) {
// Play the clip backwards.
this.prevFrame( );
} else {
// Loop back to the end if we're already at the beginning.
this.gotoAndStop(this._totalframes);
}
};See Also
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access