9.6. Function Availability and Life SpanLike variables, program functions do not live forever and are not directly accessible throughout a movie. In order to invoke functions with confidence, we need to know how to access them from different areas of our movie, and we need to be sure that they exist before we call them. 9.6.1. Function AvailabilityProgram functions (the functions we create ourselves in our code) are directly accessible for invocation only from:
By "directly accessible," we mean that the function can be invoked simply by name, without reference to a movie clip or object, like this: myFunction( ); Functions are also indirectly accessible (i.e., remotely accessible) from any point in a movie using dot syntax. As when referring to variables remotely, we must include the path to the movie clip that contains the function, such as: myClip.myOtherClip.myFunction( ); _ parent.myFunction( ); _root.myFunction( ); So, suppose a clip instance named rectangle on the main timeline contains a function named area( ). We may invoke area( ) from anywhere in our movie using an absolute path to rectangle, like this: _root.rectangle.area( ); To reference a function from a remote movie clip timeline, we follow the same rules used when referring to remote variables, as described in Chapter 2, "Variables". (We'll also learn more about invoking functions remotely in Chapter 13, "Movie Clips".) Not all functions are attached to movie clip timelines. Some program functions may be attached to user-defined or built-in objects. When attached to an object, a function is accessible only through its host object. We'll learn more about the availability and life span of methods (i.e., functions attached to objects) in Chapter 12, "Objects and Classes". 9.6.2. Function Life SpanA 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. Defining a function on the main movie timeline is the best way to ensure the function's permanence, because the main timeline persists throughout the movie. 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 may use a function invocation before the declaration of the function, as long as both are in the same script. For example: // Invoke the tellTime( ) function before its declaration statement tellTime( ); // Declare the tellTime( ) function function tellTime( ) { var now = new Date( ); trace(now); } Copyright © 2002 O'Reilly & Associates. All rights reserved. |
|