Controlling Flash MX Movie Clips
See all Sams Teach Yourself on InformIT Design & Creative Media Tutorials.
The primary element used by Flash animators and programmers alike is the movie clip. These little Flash movies inside your main Flash movie allow you to modularize your program, grouping elements and establishing levels of interactivity. Much of ActionScript is concerned with controlling movie clips.
In this hour, you will:
-
Learn how to tell movie clips what to do
-
Create an animation playback controller
-
Find out what it means to target a movie clip
-
See how to make a movie clip script
-
Make a movie clip that plays backwards
Telling Movie Clips What to Do
Suppose that you have a movie that contains a simple animated movie clip. The main movie is simple: just one frame with a movie clip on it. The movie clip itself is a long animation of some sort, but it is all self-contained inside the movie clip. It contains no ActionScript code at all.
The first thing that you need to do to control a movie clip is to name it. Naming movie clips in Flash is confusing because all movie clips have two names: the name of the original movie clip in the Library, and the name of the movie clip instance in the work area. The reason for the two names is that you can have more than one instance of the same movie clip on the screen at the same time.
So, for instance, you could have a movie clip called "gear animation" in the Library, and a "gear1" movie clip instance on the screen. You could also have a "gear2" movie clip instance. They are both taken from the original "gear animation" movie clip in the Library.
Figure 6.1 shows the property panel when a movie clip is selected. To bring up the property panel, select the movie clip in the work area and choose Window, Properties.
Figure 6.1 The properties panel shows the name of the movie clip instance and allows you to change that name if you want.
In Figure 6.1, you can see that I have named the movie clip instance "gears". It also shows the name of the original Library element as "gear animation".
The name of the movie clip instance on the screen is the critical one to an ActionScript programmer. This name is what you will need to refer to the movie clip in your programs.
For example, if you have a movie clip instance named "gears", you would refer to it with that name. Then, using dot syntax, you could give it a command, such as stop. Here is an example:
gears.stop();
You can also send other commands to a movie clip. For instance, if you want to tell a movie clip to go to a certain frame, you can use gotoAndStop.
gears.gotoAndStop(5);
Note that this is how you send commands to a movie clip that is at the current level. If you want to place a command on a frame inside this movie clip, you shouldn't use the name of the clip, just gotoAndStop. If you were to use the name of the movie clip, Flash would look for a movie clip instance named "gears" inside the current movie clip instance "gears" inside the main timelinetwo levels down rather than one.