3.7. Your First Script To prevent the movie from looping, you need to add an ActionScript method (also sometimes called a command or action). You'll use more ActionScript later, but for now, just add a simple stop( ) method and publish the movie again. In the timeline for animation.fla, add a new layer and drag it above the text and boxes layer folders. Rename the new layer actions. It is a good practice to keep the actions layer above all others in the timeline. Note: Placing your ActionScript on the top layer and naming the layer actions is a convention often used by Flash developers to make it easier to find the ActionScript in a movie (some developers name the layer "scripts" instead of "actions"). To follow best practices, don't put any content other than scripts in this layer.
To prevent graphics from being added accidentally to the actions layer, lock the layer by clicking the second dot to the right of the layer name, under the lock icon. In the last frame of the timeline, insert a keyframe in the actions layer. (Locking the layer prevents you from adding graphics to the Stage for that layer, but you can still add keyframes and ActionScript to the layer.) With the last frame still selected, open the Actions panel by choosing Window Development Panels Actions, or by pressing F9. Type stop(); into the Script pane of the Actions panel, as shown in Figure 3-15. The stop( ) method tells the playhead to stop when it reaches this frame, instead of looping. A frame script is often used to execute instructions without having to rely on user input (such as pressing a key or clicking a button). See the "Frame Scripts" sidebar for more information. Figure 3-15. The Actions panel with a stop() method added to the Script panel Save your work and test your movie again. If you haven't created the .fla yourself or are having trouble doing so, open the animation_complete.swf file in the 03 folder of your working directory and watch the final result. Frame Scripts | The frame script is the first of three main script types that you will learn about in the introductory ActionScript overviews herein. A simplified account of the frame script might describe it as serving two primary purposes. First, it is used to perform administrative tasks such as defining information that you may need later in other scripts, including variables that store data and functions that collect instructions that should be executed only when called upon. Second, a frame script is used to execute tasks passively. That is, rather than requiring active participation from the user, such as clicking a button or pressing a key, the script is executed during normal movie playback when the playhead reaches the frame in which the script resides. This is particularly useful for simple navigation methods, such as the stop( ) method used in this chapter. |
|