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, and 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 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 definitioneven if both definitions exist on the same Timelinebecause 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. 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"; Note It's 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 CallNot only do functions simply execute sets of actions but they can also be used like miniprograms 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 parametersavailableFunds and currentDayare 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 contains 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 as follows: 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. The following is the result: idealCircumstances = false; In essence, we used a function call to assign a value to the variable idealCircumstances, which 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 brace on the end, we 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 is written this way: function myNumberFunction(param1:Number, param2:Boolean):Number{ //actions } A function set up to return a string value is written this way: function myNumberFunction(param1:Number, param2:Boolean):String{ //actions } A function set up to return an Array object is written this way: function myNumberFunction(param1:Number, param2:Boolean):Array{ //actions } And so forth. If a function doesn't return a value at all (like the functions used in this lesson's projects so far), the function should be written this way: function myNumberFunction(param1:Number, param2:Boolean):Void{ //actions } Notice the use of :Void to indicate that this function does not return a value. Although the functions we used in this lesson do not make use of this syntax (they still work properly), using this syntax is considered good practice and should improve the speed of ActionScript execution. The speed increase may be noticeable only if your project contains many functions. In this exercise, using both local variables and a function that returns a value, you'll script the cable box display in our project, which displays the name of the current channel. You'll create a function that builds the text to be displayed on the cable box.
|