start page | rating of books | rating of authors | reviews | copyrights

Book HomeActionScript: The Definitive GuideSearch this book

16.2. Adding Scripts to Frames

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 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 that code to execute when the 20th frame is displayed, we must attach it to a keyframe at frame 20 of the movie. 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 once again starts playing at frame 10. This causes the movie to cycle endlessly between frames 10 and 15.

However, code on keyframes does not always control or synchronize with a movie's playhead. We may also use keyframes simply as storage devices for functions, variables, objects, and other program entities. For example, we may attach the function moveTo( ) to frame 1 of a movie clip and then invoke moveTo( ) from a button later in the movie:

function moveTo (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. We can't, for example, 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.

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, we use either the movie clip _framesloaded property or the getBytesLoaded( ) method. For sample code, see the entry for Movieclip._framesloaded in Part III, "Language Reference".



Library Navigation Links

Copyright © 2002 O'Reilly & Associates. All rights reserved.