9.6 Function Availability and Life Span

ActionScript for Flash MX: The Definitive Guide, 2nd Edition
By Colin Moock
Chapter 9.  Functions

Like variables, program functions do not live forever and are not directly accessible from everywhere in 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. In ActionScript, calling a nonexistent function has no effect. The call fails silently and does not produce an error (in JavaScript, calling a nonexistent function generates an error). To test whether a function ran when you called it, add a trace( ) statement, such as the following, to the function:

trace("Function someName executed.");

9.6.1 Function Availability

Custom program functions (the functions we create in our code) are directly accessible for invocation only from:

  • Code attached to the timeline of the movie clip that bears the function declaration

  • Code in a Flash 5-style onClipEvent( ) handler on the movie clip that bears the function declaration

  • Code in a Flash 5-style on( ) event handler on a button on the timeline of the movie clip that bears the function declaration

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), using dot syntax, from any point in a movie. 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 can 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. (We'll also cover more about invoking functions from other movie clips in Chapter 13.)

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 discuss the availability and life span of methods (i.e., functions attached to objects) in Chapter 12.

To create a function that is directly available throughout an entire movie, define it as a function literal on the _global object, as follows:

_global.functionName = function ( ) {   statements }

Global functions and variables were not supported prior to Flash Player 6. For more information on globals, see _global in the Language Reference.

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.

9.6.2 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 persists throughout the movie. As of Flash 6, the most permanent location for a function 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 function 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 () {   var now = new Date();   trace(now); }

This technique works within a single keyframe's script but not across timeline layers. Code on different layers is executed independently, according to the timeline's order of execution, discussed in Chapter 13.

Because function literals are not available prior to declaration, even in the script in which they are defined, we must define function literals prior to their invocation. As a best practice, you should define functions at the beginning of your scripts. This ensures they will be available for the entirety of the script, even if they are declared as function literals.

     



    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