Flash 8: Projects for Learning Animation and Interactivity (OReilly Digital Studio)
6.3. More Movie Clip Control
In addition to using a simple stop() command, you can also allow the user to control Box Guy if you implement the necessary functionality. For example, you can tell Flash to restart the animation when the user clicks on the character. To accomplish this, add an event handler to your ActionScript. An event handler tells the Flash Player, "When an eventsuch as the user clicking on the characteroccurs, perform the following action(s)." In this case, the desired action is to replay the animation. How do you do this? Whenever the user clicks on Box Guy, you'll tell the main timeline to play again, and send the same instructions to the legs. Since timelines automatically loop by default, this will keep both timelines playing until the next stop instruction is received. Here's how it's done: Note: Note that, as is a best practice, you placed this code in the main timeline, not the clip's timeline. Because the script is written in the main timeline, the clip paths are relative to the main timeline. This means that the play() command instructs the main timeline to play. (If you haven't read the "Absolute and Relative Target Paths" sidebar, you may want to do so now.)
The script you wrote sets up a function that runs when you click on the box_ guy_mc clip in the movie. In the previous chapter, we briefly discussed how to write and call a function. If you look closely, this format is a bit different. These functions do not have names, and are on the right side of an equation. In this format, the function is directly assigned to the event handler.
These are called anonymous functions, and because they don't have names, they can't be called in the traditional sense. In this case, they are assigned directly to an event handler and are automatically called when the event in question (here, a mouse press) occurs. In future chapters, you will see that event handlers can also call named functions. This is convenient when the function must be called from other locations as well, or when two different event handlers must call the function. 6.3.1. What's Next?
Before moving on to the next chapter, try using your new ActionScript knowledge to make the character's eyes stop blinking. (Hint: You'll need to give instance names to the on-Stage instances of the face_mc and eye_mc movie clip symbols so you can target them from ActionScript, and you'll need to stop each eye_mc instance separately.) If you feel inspired, try to improve Box Guy's walk cycle by making sure the first and last frames of the movie clip do not appear to be identical. See the last Note in "Making Him Walk" for tips on how to do so. Finally, flip the character horizontally (by selecting the movie clip instance and choosing Modify Movie clips are arguably the most powerful objects in Flash. They can be used for independent animations, such as the one you created in this chapter, or as self-contained pieces of functionality and logic for a movie. For example, later in this book, you'll use a movie clip to create a preloader for external assets that load into a Flash movie while it's running. (A preloader gives users a visual progress update when an external asset is being loaded.) The preloader clip contains nothing but ActionScript and a text field. Another great benefit of using movie clips instead of animating everything in the main timeline is that clips can be used in more than one project. If you plan to create an entire series of cartoons using the same characters, for example, you can create walk cycles for each character and simply import them into each new movie. In the next chapter, you'll use what you've learned to build an animated ad. You'll focus on:
|