EasyManua.ls Logo

MACROMEDIA FLASH 8-LEARNING ACTIONSCRIPT 2.0 IN FLASH - Page 218

MACROMEDIA FLASH 8-LEARNING ACTIONSCRIPT 2.0 IN FLASH
830 pages
Print Icon
To Next Page IconTo Next Page
To Next Page IconTo Next Page
To Previous Page IconTo Previous Page
To Previous Page IconTo Previous Page
Loading...
218 Functions and Methods
Using variables in functions
Local variables are valuable tools for organizing code and making it easy to understand. When
a function uses local variables, it can hide its variables from all other scripts in the SWF file;
local variables are invoked in the scope of the body of the function and cease to exist when the
function exits. Flash also treats any parameters passed to a function as local variables.
To use variables in functions:
1. Create a new Flash document and save it as flashvariables.fla.
2. Add the following ActionScript to Frame 1 of the main Timeline:
var myName:String = "Ester";
var myAge:String = "65";
var myFavSoftware:String = "Flash";
function traceMe(yourFavSoftware:String, yourName:String,
yourAge:String) {
trace("I'm " + yourName + ", I like " + yourFavSoftware + ", and I'm "
+ yourAge + ".");
}
traceMe(myFavSoftware, myName, myAge);
3.
Select Control > Test Movie to test the Flash document.
For more information on passing parameters, see “Passing parameters to a function
on page 218. For more information on variables and data, see Chapter 4, “Data and Data
Types,” on page 71
Passing parameters to a function
Parameters, also referred to as arguments, are the elements on which a function executes its
code. (In this book, the terms parameter and argument are interchangeable.) You can pass
parameters (values) to a function. You can then use these parameters for processing the
function. You use the values within the function block (statements within the function).
Sometimes parameters are required, and sometimes they are optional. You might even have
some required and some optional parameters in a single function. If you do not pass enough
parameters to a function, Flash sets the missing parameter values to
undefined, which may
cause unexpected results in your SWF file.
The following function called
myFunc() takes the parameter someText:
function myFunc(someText:String):Void {
trace(someText);
}
NOTE
You can also use regular variables in a function. However, if you modify regular variables,
it is good practice to use script comments to document these modifications.

Table of Contents

Related product manuals