Tải bản đầy đủ (.pdf) (10 trang)

Using Local Variables and Creating Functions that Return Results

Bạn đang xem bản rút gọn của tài liệu. Xem và tải ngay bản đầy đủ của tài liệu tại đây (25.97 KB, 10 trang )


< Day Day Up >

Using Local Variables and Creating Functions that Return Results
The variables you've created and used so far can be accessed at any time by any script in
the Flash movie. In contrast, local variables are special variables you can create and use
only within the scope of a function definition. In other words, a local variable is created
within the function definition, used by the function when it's called, then deleted
automatically when that function has finished executing. Local variables exist only within
the function where they are created.
Although local variables are not absolutely required in ActionScript, it's good
programming practice to use them. Applications that require many and frequent
calculations create a lot of variables and will slow applications over time. By using local
variables, however, you minimize memory usage and help prevent naming collisions,
which occur when your project gets so big you unknowingly create and use variable
names that are already in use. However, local variables in one function definition can
have the same names as local variables within another function definition—even if both
definitions exist on the same timeline. This is because Flash understands that a local
variable has meaning only within the function definition where the variable was created.
There is only one way to create a local variable manually, and you have been using this
syntax for four lessons. Here's the syntax:

var myName:String = "Jobe";


This variable becomes a local variable by simply being declared within a function
definition, using the keyword var.
To better grasp this concept, consider this example.
In the previous exercise, we declared (created) the variable currentChannel on Frame 1 of
the main timeline using the following syntax:


var currentChannel:Number;


Because the line of script that created the variable was on Frame 1 of the main timeline,
and it didn't exist within a function definition, currentChannel became a variable of the
main timeline. If we place this exact syntax within a function definition, currentChannel
is considered a local variable (belonging to the function only); it exists only when the
function is called and is deleted immediately upon the completion of the function's
execution. Think of local variables as temporary variables, for use within functions.
If you need to create a timeline variable from within a function, do not use the var syntax
when declaring it. Declare the variable like this:

name = "Jobe";


TIP
It is best to create timeline variables outside of function definitions. Declaring a timeline
variable outside of a function is considered good practice because you group all your
timeline variables together. When coming back to your code months later or having
another programmer look at your code, this variable organization will be appreciated.

Multiple local variables can be declared within a function definition on a single line using
this syntax:


var firstName:String = "Jobe", lastName:String = "Makar", email:String =
"jobe@electrotank

.com";



Returning Results from a Function Call
Not only do functions simply execute sets of actions; you can also use them like mini-
programs within your movie, processing information sent to them and returning values.
Take a look at this function definition:

function buyCD(availableFunds:Number, currentDay:String):Boolean{

var myVariable:Boolean;

if(currentDay != "Sunday" && availableFunds >= 20){

myVariable = true;

}else{

myVariable = false;

}

return myVariable;

}


The values of two parameters—availableFunds and currentDay—are sent to the function
when it is called. The function processes those values using an if/else statement. At the
end of this function, myVariable will contain a value of true or false. Using the return
statement (as shown at the bottom of the function definition), the value of myVariable is
returned to where the function was called. To understand this, take a look at how this

function is called in the following script:

var idealCircumstances:Boolean = buyCD(19, "Friday");

if(idealCircumstances == true){

gotoAndPlay("Happiness");

}else{

gotoAndPlay("StayHome");

}


Pay particular attention to the line that reads:

var idealCircumstances:Boolean = buyCD(19, "Friday");


To the right of the = sign is our actual function call, which sends the values of 19 and
"Friday" to the buyCD() function for processing. If you recall how our function was
defined, these values are used to determine a true or false value for myVariable. Sending
these particular values (19, "Friday") to the function causes myVariable to evaluate to a
value of false. Because the last line of code in our function says return myVariable;, the
value of myVariable is returned to the script where the function was called. So,

idealCircumstances = false;



In essence, we used a function call to assign a value to the variable idealCircumstances.
This happens in a split second. After a value has been assigned, the value of
idealCircumstances can be used in the rest of the script, as our example demonstrates.

TIP
You can use the return action to return any data types, including variable values, arrays,
or any other objects.

Now that you understand that functions can return values, it's a good time to point out a
minor addition to our function definition syntax. The first line of our buyCD() function
definition looks like this:

function buyCD(availableFunds:Number, currentDay:String):Boolean{


Between the closing parenthesis and the curly bracket on the end, we've placed the syntax
:Boolean. This addition is to indicate that the function returns a value whenever it is
called. In this case, the function returns a true or false value, hence the reason for using
:Boolean. A function set up to return a numeric value would be written this way:

function myNumberFunction(param1:Number, param2:Boolean):Number{

//actions

}


A function set up to return a string value, this way:

function myNumberFunction(param1:Number, param2:Boolean):String{


//actions

}


A function set up to return an Array object, this way:

function myNumberFunction(param1:Number, param2:Boolean):Array{

//actions

}

×