- ActionScript Elements Used to Make Games and Toys
- Lesson 15: Controlling the Playback of a Movie Clip
- Lesson 16: Controlling the Properties of a Movie Clip
- Lesson 17: Dragging and Dropping Movie Clips
- Lesson 18: Movie Clips and Levels
- Lesson 19: Duplicating Movie Clips
- Lesson 20: Controlling Multiple Movie Clips
- Lesson 21: Detecting Collisions
- Lesson 22: Using Movie Clips to Change What Is on the Screen
- Lesson 23: Accepting Keyboard Input
- Lesson 24: Playing Sounds
Lesson 15: Controlling the Playback of a Movie Clip
Example file: Mcplayback.fla
Movie clips are like extra little Flash movies inside your main Flash movie. They contain a complete timeline with layers and most of the elements of your main Flash movie. You can control your main Flash movie with commands such as play() and stop(). You can also control a movie clip with these commands.
On the CD, in a file named Mcplayback.fla, you'll see a movie clip named MyMovieClip in the library. This movie clip contains 10 frames labeled with some text so that you know which frame of the movie clip is being shown at which time.
By dragging that movie clip from the library to the Stage, you are creating an instance of that movie clip. You can and should name these instances. To do this, use the Property inspector. In Figure 3.1, you can see the Property inspector used to name the instance myMovieClip.
Figure 3.1 The Property inspector allows you to name movie clip instances.
TIP
You might have noticed that the movie clip instance is named the same as the movie clip in the library. This is commonly done when you are creating exactly one instance of a movie clip and have no plans to create more. The name of the instance is easier to remember when it is the same as the library item.
The example movie also has five buttons in the library. These have been placed on the Stage just under the movie clip instance. You don't need to name the button instancesin fact, you can't. Unfortunately, buttons by themselves cannot be referenced by code in any way, so there is no need for a name.
After the movie clip and the five buttons are on the Stage, you should get something that looks like Figure 3.2. The buttons look like video tape recorder controls, and that's exactly how they will be used.
Figure 3.2 The buttons allow you to control the playback of the movie clip above them.
The five buttons have complete control over the movie clip. The play button starts the movie clip moving forward. The clip continues to move forward, looping around to the beginning when it reaches the end. The stop button halts the movie clip at its current frame. The rewind button takes the movie clip back to frame one and stops it. The previous and next buttons advance the movie clip one step and stop it there.
Before the movie turns over control of the movie clip to the user, it must tell the movie clip to stop moving. Movie clips naturally want to start going as soon as they appear on the Stage. To prevent this, the following frame script was placed on the first frame of the main timeline. It stops the movie clip from animating, and also tells the main movie to stop.
myMovieClip.stop(); stop();
By using the dot-syntax, the first line tells the movie clip instance named "myMovieClip" to obey the stop() command. The second line has no instance name before it, so the command is issued to the same place where the script is locatedin this case, the main timeline.
The button scripts use the same dot-syntax to send their commands to the movie clip. Here is the play button's script, which tells the movie clip to play():
on (press) { myMovieClip.play(); }
The stop button sends the stop() command to the movie clip.
on (press) { myMovieClip.stop(); }
The rewind button needs to tell the movie clip to return to the first frame and stop there. The gotoAndStop() command with the parameter 1 does just that.
on (press) { myMovieClip.gotoAndStop(1); }
The previous and next buttons move the movie clip backward or forward one frame. The prevFrame() and nextFrame() commands can be used for this. Here are both scripts:
on (press) { myMovieClip.prevFrame(); } on (press) { myMovieClip.nextFrame(); }
Although the purpose of this lesson is to teach you how to control the playback of movie clips, you can actually use the method in this example as a way to play back an animation or even a slideshow with Flash.