
This is the Title of the Book, eMatter Edition
Copyright © 2007 O’Reilly & Associates, Inc. All rights reserved.
186
|
Chapter 9: Functions
Once defined, we can access a function’s parameter values from inside the function
body just as we would any other variable. For example, here is a function with one
parameter:
function say (msg) { // Define the msg parameter
trace("The message is " + msg); // Use msg within the trace( ) statement
}
Our function declaration defines the parameter msg. The trace() statement uses the
msg parameter as it would any variable, in this case displaying its value to the Output
window.
Let’s use parameters with our function so that we can set the clip’s name, horizontal
distance, and vertical distance differently each time it runs. Example 9-1 shows the
revised code.
We renamed the function from moveBall( ) to the generic name moveClip( ) to reflect
its ability to work with any clip. We also defined three parameters:
theClip (the
movie clip we want to move),
xDist (the horizontal distance to move), and yDist (the
vertical distance to move). Inside the function body, we use those parameters instead
of the literal “hardcoded” values in the earlier moveBall() example, thereby allowing
our function to reposition any clip by any horizontal and vertical distance. Notice
that we no longer need the global variable
mainTimeline because the function is told
which clip to move when it