Controlling the Playback Speed and Direction of a Timeline
Normally, a movie's timeline plays in a forward direction at a pace dictated by the fps (frames per second) setting in the Movie Properties dialog box. However, you can control the direction in which the timeline moves as well as its speed by using ActionScript. In fact, by combining scripting elements (which control direction) with the onEnterFrame event, you can gain incredible control over the project's timeline.The first two scripting elements we'll discuss are the nextFrame() and prevFrame() methods of the MovieClip class. You use these methods to move a timeline to the next or previous frame by employing the following syntax:
or
myButton_btn.onRelease = function(){
myMovieClip_mc.nextFrame();
}
By assigning these event handlers to buttons, you can create navigation controls that automatically advance or rewind a timeline one frame at a time with each click of the button.Even more powerful is a timeline's _currentframe property (a read-only property): its value represents the frame number at which the playhead currently resides. For example, if the main movie is being played, the following script places the numerical value of the playhead's current frame position into a variable named whereAreWe:
myButton_btn.onRelease = function(){
myMovieClip_mc.prevFrame();
}
var whereAreWe:Number = _root._currentframe;

In this script, the actions within the conditional statement are executed only when the playhead on the main timeline is between Frames 50 and 100.Using the _currentframe property in conjunction with a gotoAndPlay() action allows you to control the direction as well as the pace in which the timeline moves. In the following example, the playhead on the main timeline advances 10 frames from its current position:
_root.onEnterFrame = function() {
if(this._currentframe >= 50 && _this.currentframe <= 100)
// Perform these actions
}
}
In the same way, you can use the following script to make the timeline move backward 10 frames:
_root.gotoAndPlay (_currentframe +10);
As the following exercise demonstrates, by using the onEnterFrame event to execute a line of script such as the one above, you can create fast forward and rewind buttons to control a timeline's playback.
_root.gotoAndPlay (_currentframe -10);
Open makeMyDay3.fla.We'll continue to build on the project from the preceding exercise; most of the work for this exercise focuses on the Messages section of the application. In this area are four buttons named play_btn, stop_btn, ff_btn, and rew_btn, and a graphically empty movie clip named messages_mc. The four buttons eventually will be scripted to control the messages_mc timeline. Let's take a closer look at that timeline.

The first line of the script creates a Sound object named fastSound. This Sound object eventually will be scripted to play a fast-forwarding sound when the messages_mc timeline is fast-forwarded or rewound.The remaining lines of the script are the beginnings of the handleMessages() function. This function accepts a single parameter named action, which contains a string value of "play", "stop", "ff", or "rew". This parameter value tells the function whether it should play, stop, fast-forward, or rewind the messages_mc timeline. As we'll describe shortly, this function will be called and passed various parameter values when you interact with one of the playback control buttons.Insert the following conditional statement within the handleMessage() function definition:
var fastSound:Sound = new Sound(messages_mc);
function handleMessages(action:String){
}
if(action == "play"){
fastSound.stop()
delete messages_mc.onEnterFrame;
messages_mc.play();
}else if(action == "stop"){
fastSound.stop();
delete messages_mc.onEnterFrame;
messages_mc.stop();
}else{
fastSound.attachSound("rewind");
fastSound.start(0, 50);
if(action == "ff"){
messages_mc.onEnterFrame = function(){
this.gotoAndStop(this._currentframe + 3);
}
}else{
messages_mc.onEnterFrame = function(){
this.gotoAndStop (this._currentframe - 10);
}
}
}


These lines of script assign event handlers to the various playback control buttons. How this works should be obvious to you by now. The only thing to be aware of is that both the ff_btn and rew_btn instances have an onPress and an onRelease event assigned. This allows those buttons to fast-forward or rewind when pressed, but to automatically initiate normal playback when released.It's time to test the final result.Choose Control > Test Movie to view the project to this point.When the movie appears, use the various buttons to control the playback of the messages_mc movie clip instance. Clicking the FF button moves that timeline forward quickly; clicking the Rew button moves it backward quickly (as is evident from the points where the various sound clips in the instance are heard).Close the test movie and save the project as makeMyDay4.fla.Playback control buttons such as the ones we set up here have a wide range of usesnot just for streaming sounds (as demonstrated) but for any kind of project whose timeline contains hundreds of frames of content. By making use of these buttons, you enable users to control the way they view your projectsa powerful capability.
play_btn.onRelease = function(){
handleMessages("play");
}
stop_btn.onRelease = function(){
handleMessages("stop");
}
ff_btn.onPress = function(){
handleMessages("ff");
}
ff_btn.onRelease = function(){
handleMessages("play");
}
rew_btn.onPress = function(){
handleMessages("rew");
}
rew_btn.onRelease = function(){
handleMessages("play");
}