16.2 Adding Scripts to Frames

ActionScript for Flash MX: The Definitive Guide, 2nd Edition
By Colin Moock
Chapter 16.  ActionScript Authoring Environment

Flash documents are fundamentally structured around the concept of animation. Every Flash document comprises a linear sequence of frames, or slices of visual and audio content. A great deal of ActionScript code is tied to this frame-based structure. By placing a code block on a frame, we stipulate the timing of that block's execution relative to the playback of the movie. Whenever we add code to a frame, we must always consider not only what we want the code to do, but also when we want the code to execute.

For example, we may want to write some code that places a new movie clip on stage when the 20th frame of a movie is displayed:

_root.attachMovie("myClip", "clip1", 0);

In order for this code to execute when the 20th frame is displayed, we must attach it to a keyframe at frame 20 of the movie.

ActionScript code attached to a frame must reside in a keyframe. Use Insert figs/u2192.gif Keyframe or Insert figs/u2192.gif Blank Keyframe to create a keyframe to which your code can be attached.

To attach code to a keyframe, we select the keyframe in the timeline, open the Actions panel, and then add the desired code to the Script pane. During playback, code on a keyframe is executed before the content of the frame is displayed.

Code on keyframes is used to perform tasks synchronized to the movie's playback and to establish program elements such as variables, functions, and objects for use throughout a movie clip or movie. A timeline loop is a simple example of a synchronized task. Suppose we attach the following code to frame 15 of a movie:

gotoAndPlay(10);

When the playhead of the movie reaches frame 15, the code executes, and the movie begins playing back at frame 10 again. When frame 15 is reached for the second time, the code is executed again, and the movie again starts playing at frame 10. This causes the movie to cycle endlessly between frames 10, 11, 12, 13, 14, and 15.

However, code on keyframes does not always control or synchronize with a movie's playhead. We can also use keyframes to represent application states (e.g., as a stopping point in a movie, such as for each quiz question in Chapter 1), or simply as storage devices for functions, variables, and objects. For example, we can attach the function moveClipTo( ) to frame 1 of a movie clip and invoke moveClipTo( ) from a button later in the movie:

function moveClipTo (x, y) {   _x = x;   _y = y; }

One caution: because the execution of code on frames is dictated by the playback of a movie, we must always ensure that variables, functions, and other program elements are available before they are accessed. For example, we can't invoke a function at frame 3 if that function isn't defined until frame 10. Code used globally throughout a movie should therefore be placed on the first frame of the main timeline or on the first frame after the movie's preloader. See the _global object in the Language Reference for more information on global code placement. See also Chapter 2 and Chapter 9 for a discussion of variable and function scope.

We must also ensure that no code attempts to access portions of a movie that have not yet loaded. To check whether a specific portion of a movie has loaded, use either the movie clip _framesloaded property or the getBytesLoaded( ) method. For sample code, see the entry for Movieclip._framesloaded in the ActionScript Language Reference.

     



    ActionScript for Flash MX. The Definitive Guide
    ActionScript for Flash MX: The Definitive Guide, Second Edition
    ISBN: 059600396X
    EAN: 2147483647
    Year: 2002
    Pages: 780
    Authors: Colin Moock

    flylib.com © 2008-2017.
    If you may any questions please contact us: flylib@qtcs.net