10.6. Playing the Timeline Backward

Problem

You want to play the timeline backward.

Solution

Use the prevFrame( ) action within an onEnterFrame( ) event handler method in combination with bounds checking.

Discussion

By default, each movie clip timeline (including the main timeline) plays back in a forward direction. And Flash does not have a built-in function or method that can automatically instruct a timeline to play in reverse. However, with just a bit of ActionScript code, you can cause a timeline to play backward.

The basic idea is as follows: at the frame rate of the movie, tell Flash to move the playhead back one frame. Instructing the playhead to move back one frame is fairly simple. The only challenge is to then tell Flash to call the prevFrame( ) method at the frame rate of the movie. That too is fairly simple, because there is a built-in event handler method named onEnterFrame( ) that gets called at the frame rate of the movie. Therefore, in order to instruct a timeline to play in reverse, you should define the onEnterFrame( ) event handler for the movie clip such that it calls prevFrame( ). The following example instructs a movie clip named mAnimation to play backward:

	mAnimation.onEnterFrame = function():Void {
	  this.prevFrame();
	};

As you can see, you can define an onEnterFrame( ) event handler method, just as with the other already-familiar event handler methods such as onPress( ) and onRelease( ). Also notice that within the event handler method, you can refer to the movie ...

Get Flash 8 Cookbook now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.