- Controlling the Timeline
- Basic Timeline Stop and Play Scripts
- Navigating to Specific Frames
Basic Timeline Stop and Play Scripts
-
Open the stopAndPlay_final.fla file in Flash. This is a working version of the project you’ll be building in this exercise.
-
Choose Control > Test Movie to preview the movie (See Figure 1). In the SWF file, click the Stop button to stop the animation and the Play button to start the animation up again. You will learn how to add the same functionality to this movie in the following steps.
Figure 1 An SWF file with working scripts for controlling playback
- After you finish stopping and playing this movie, close the Preview window and the FLA file.
- Open the stopAndPlay.fla file in Flash. This is an unfinished version of the
movie you just previewed—it contains everything except the ActionScript,
which you will add next (see Figure 2).
Figure 2 The starting FLA file without working scripts for controlling playback
- Choose Control > Test Movie to preview the movie. Click the Stop and Play buttons. Notice that nothing happens and the movie continues to play. Why? No actions have been added to these buttons yet, and therefore the buttons do not control the movie. You will learn to do this next.
- Close the Preview window and return to the project file. Click the Stop
button instance on the Stage to select it. In the Instance Name box of the
property inspector enter stop_btn (see Figure 3).
Figure 3 Always provide a unique name to your symbol instances.
- Choose Window > Actions (F9) to open the Actions panel. Notice that the
top of the Actions panel reads Actions – Button (see Figure 4). Because
you have selected the button instance, Flash 8 knows that you will be adding
actions to the button instance on the Stage.
Figure 4 The Actions panel when a button is selected
-
In the Actions panel, click the Script Assist button and then the Global Functions category in the Toolbar to access a list of all the actions that fall under that category. Click Movie Clip Control to expand that category. Double-click the on action to add it to the Script pane (see Figure 5). From the Script Assist parameters that appear above the script pane, choose "release" for the Event setting.
-
Figure 5 Adding an event handler to the first line of the stop button script
- With the first line of script still selected in the Actions pane, choose
Global Functions > Timeline Control and double-click the
stop action to add it to the Script pane (see Figure 6). The Stop
button instance now has a working script (hopefully) that will stop the
Timeline.
Figure 6 The correct completed script for the stop button
You can also add an action to the list by dragging it from the Toolbar to the Script pane or by selecting the Add (+) button and choosing the action from the pull-down menus, as shown in Figure 7.
Figure 7 An alternative method of adding actions to the script pane
When you double-clicked the stop action in the Actions pane, ActionScript appeared in the Script pane on the right. The Script pane of the Actions panel holds the ActionScript statements and displays all the code for the actions that are applied to an object.
The stop action has no parameters, so in this case the area above the script pane was empty (refer to Figure 6).
- This is a good time to test the script. Whenever possible, always
test each line of code as you add them to the Script pane. Choose Control >
Test Movie and try out the stop action you just added to the button instance.
When you click the Stop button, the movie should stop.
Once a movie is stopped, it must be explicitly started again in order to play. You will do this next with the play action.
-
Close the Preview window after you finish. On the Stage, click the Play button instance to select it. In the Actions panel, add the on handler again by choosing Global Functions > Movie Clip Control and then double-clicking the on action to add it to the Script pane. Choose Event: Release from the selection of parameters listed above the script pane (see Figure 8).
-
Figure 8 Adding the event handler for the Play button
-
With the first line still selected, choose Global Functions > Timeline Control and double-click the play action to add it to the Script pane (see Figure 9).
-
Figure 9 The complete scripts for the Play button
The play button now hopefully has a working script for restarting Timeline playback.
- Choose Control > Test Movie and test the movie again. Click the Stop
button to stop the movie. Click the Play button to make the movie play again.
When you finish, close the Preview window.
As in the stopAndPlay_final.fla file you previewed earlier, the movie immediately plays as soon as the Preview window opens. By default, the Main Timeline in the movie will automatically begin to play unless you tell it otherwise.
However, often you will not want your movies to start playing automatically. In fact, one of those "must-learn" scripts I mentioned earlier are those scripts for stopping Flash playback. You have already learned how to use a button to accomplish this; now you learn how to use a keyframe script to do this. Add a stop action to the keyframe in the Timeline to have the movie stop before it begins playing. Then the movie will not play until the Play button is clicked.
-
Back in the Main Timeline, click the Insert Layer button to add a new layer to the Timeline; name it actions.
-
In the Main Timeline, select Frame 1 of the actions layer. Notice there is a blank keyframe there. In the Actions panel, double-click the stop action to add it to the Script pane (see Figure 10).
-
Figure 10 The blank keyframe in frame 1 now has a small "a" to indicate it has a script in it.
- Test the movie one last time. The movie should now open in a paused state, and the play and stop buttons should still be working the same as before.