|
Free Open Book
Macromedia Flash 8 Bible |
Overview of Functions as ProceduresA primary building block of any scripting or programming language is a procedure. A procedure is any set of code that you wish to reserve for a specific task. A procedure is useful for code that you wish to reuse in multiple event handlers (for example, Button instances and keyframes). In Flash ActionScript, procedures are called functions, and are created with the function action. What Functions DoA function (or procedure) sets aside a block of code that can be executed with just one line of code. Functions can execute several actions, and pass options (called arguments or parameters) to those actions. All functions must have a unique name, so that you know what to reference in later lines of code. In a way, functions are equivalent to your own custom additions to the Flash ActionScript language. In ActionScript, you can define a function on a specific timeline, and refer to its path and name to execute it.
When to Create a FunctionFor people new to scripting, perhaps the most confusing aspect of functions is knowing when to create them in a Flash movie. Use the following guidelines to help you determine when a function should be created:
How to Define a FunctionWhen you add a function to a keyframe on the Main Timeline or a Movie Clip timeline, you are defining the function. All functions have a target path, just like other objects in ActionScript. All functions need a name followed by opening and closing parentheses, but arguments (options to pass to the function) inside the parentheses are optional.
As a simple example, let's say you want to create a function that has one gotoAndStop() action. This function will have a shorter name than gotoAndStop(), and will be faster to type and use in your ActionScript code. Place this code on the first keyframe of the Main Timeline.
function gts():Void {
this.gotoAndStop("start");
}
This function, when evoked, will send the current timeline Playhead to the start label You could further expand the functionality of gts() by adding an argument, which we'll call frameLabel:
function gts(frameLabel:String):Void {
this.gotoAndStop(frameLabel);
}
In this version of the gts() function, instead of hard-coding a frame label such as start into the actual gotoAndStop() action, you specify an argument with the name frameLabel. Just like variable names, the names of your function and its arguments are entirely up to you — the name frameLabel has no significance. ActionScript simply knows that if you pass an argument to the gts() function, that it should place that argument where the frameLabel term occurs in your actions. An argument acts as a placeholder for information that will be supplied to the function on a per-use basis; that is, you can specify a different value for frameLabel each time you evoke the gts() function. You can also use as many (or as few) arguments as you need. Insert a comma between each argument in the function declaration:
function calculateRange (min:Number, max:Number):Number{
var diff:Number = Math.abs(max - min);
return diff;
}
How to Execute a FunctionAfter you have defined a function on a timeline's keyframe, you can create actions that refer to the function's actions. The standard method for executing a function is: At the end of the previous section, you defined a function named gts() on the Main Timeline. If you added a Button instance to your movie, you could then execute the function from the Button instance with the following code:
on(release){
this.gts("start");
}
When this Button instance is clicked, the function gts() on the current timeline (this) is executed, and passed the argument "start". In your function gts(), you defined frameLabel as an argument that occurs in the gotoAndStop() action. Therefore, the Main Timeline will go to and stop on the "start" frame label.
|
Main Menu |
| 500 Juegos Gratis | 500 Giochi Gratis | 500 Jeux Gratuits | 500 Jogos Gratis | 500 Kostenlose Spiele |