
This is the Title of the Book, eMatter Edition
Copyright © 2007 O’Reilly & Associates, Inc. All rights reserved.
194
|
Chapter 9: Functions
How do you know where to define your functions? If the function is
going to be used just for a single movie clip, define it on the movie clip
of interest. If the function is going to be used by multiple movie clips,
define it on the main timeline as a global function. We’ll discuss code
organization when we study object-oriented programming in
Chapter 12.
Function Life Span
A function defined on a movie clip timeline is lost when that clip is removed from
the Stage. An attempt to invoke a function that was defined in a clip that no longer
exists will fail silently. In Flash 5, defining a function on the main movie timeline is
the best way to ensure the function’s permanence, because the main timeline per-
sists throughout the movie. As of Flash 6, the most permanent location for a func-
tion is on the
_global object, as described in the previous section.
Note that functions in a frame’s script are initialized and become available when the
playhead first enters the frame containing the script. Therefore, we can use a func-
tion invocation before the declaration of the function, as long as both are in the same
keyframe’s script. For example:
// Invoke the tellTime() function before its declaration statement
tellTime();
// Declare the tellTime() function
function tellTime ...