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

Tài liệu Adding Parameters to functions docx

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 (31.13 KB, 13 trang )


< Day Day Up >

Adding Parameters to Functions
In the preceding exercise, you learned how to create a function and call it. In this
exercise, you'll add parameters to a function and learn how to use them. Here's the syntax
for creating a function that accepts parameters:

function convertToMoonWeight (myWeight:Number){

var weightOnMoon:Number = myWeight/6.04;

}


The sole parameter for this function is named myWeight. The value of that parameter is
also used within the function definition (near the end of the second line), just as if it were
a preexisting variable. Notice that you should associate a data type with the parameter in
the function definition. In this case, the parameter myWeight represents a numeric value.
Here's an example of the syntax used to call this function:

convertToMoonWeight(165);


Here you can see that we added a numeric value of 165 to the function call. This value is
sent to the function being called so that it can perform its specified functionality, based
on that value. In this example, the value of 165 in the function call is assigned to the
myWeight parameter in the function definition. The result looks something like this:

function convertToMoonWeight (165){


var weightOnMoon:Number = 165/6.04;

}


Thus, in our example, sending a value of 165 to our convertToMoonWeight() function
would set the value of weightOnMoon to 165/6.04, or 27.32.
The myWeight parameter is replaced with the value sent to the function when it is called.
The great thing about this is that whenever we call our function again, we can send it a
different value, which will result in the weightOnMoon variable's being set to a different
value as well. Take a look at these function calls to the convertToMoonWeight()
function:

convertToMoonWeight(190);

convertToMoonWeight(32);

convertToMoonWeight(230);


Each of these function calls is to our single function, but because different values are sent
to the function in each call, the function performs the same action (converting that value
to moon weight) using the different values.
NOTE
Parameters you define for a function have meaning only within that function. In our
example, myWeight has no meaning or use outside of the function itself.

When sending values to a function, you can also use variables in your function call, as in
the following:


convertToMoonWeight(myVariable);


When you do this, the current value of the variable is passed to the function.
Functions can also be made up of multiple parameters, like this:

function openWindow(url:String, window:String){

getURL(url, window);

}


This function definition contains two parameters, url and window, separated by a comma.
The function contains a single action, getURL(), which makes use of these two
parameters. Making a call to this function looks like this:

openWindow("", "_blank");


The function call also sends two values, separated by a comma, to the function: a URL
and the HTML target for opening the specified URL. These parameter values are used by
the function in order to perform its specified functionality. In this case, the function call
would result in yahoo.com opening in a new browser window.
When defining multiple parameters in a function definition, remember their order within
the parentheses. Respective values that are defined in the function definition should be
listed in that same order in the function call.
Here's a function call to the openWindow() function that won't work because the
parameters of the function definition dictate that the URL should be listed first in the
function call:


openWindow("_blank", "");


NOTE
When a function is called, a temporary array called arguments is created. This array
contains all parameters passed to the function—even if you specified none when defining
your function. Here is an example of how to access the arguments array:

function traceNames() {

trace("This function was passed " + arguments.length + "arguments");

trace("The value of the first argument is: " + arguments[0]);

trace("The value of the second argument is: " + arguments[1]);

}

traceNames("Kelly","Chance");


In this example, these strings appear in the output window:

This function was passed two arguments

The value of the first argument is: Kelly

The value of the second argument is: Chance




Accessing the arguments array enables you to create functions that can adapt their
functionality based on how many parameters are passed to them. For more information
about arrays, see Lesson 6
, "Creating and Manipulating Data."
In this exercise, you'll add functionality to the numeric keypad on the remote control and
to the TV channel Up and Down buttons, allowing them to change the channel displayed
on the TV screen. The numeric buttons work by calling a function and passing in the
number of the channel to jump to. You will also modify the togglePower() function
slightly.
1. Open television2.fla.
We continue to work with the file you completed in the preceding exercise. Before
we do, however, it's important to note the structure of the screen_mc movie clip
instance, which is inside the tv_mc movie clip instance. The screen_mc movie clip
instance's timeline has graphical content on Frames 1 through 7. This content
represents the "off" state of the TV (on Frame 1), as well as six channels of
programming that our TV will be set up to receive.

2. Select Frame 1 of the Actions layer on the main timeline and open the Actions
panel. Add this ActionScript just below the line that reads var tvPower:Boolean =
false;:
3.
4. var currentChannel:Number;
5.

In this exercise we create functions that change the channel of the TV, including
incrementing and decrementing the TV channel. To increment or decrement a
channel, you need to have the current channel stored. The script declares a new
variable called currentChannel, which will be used to store the numeric value of

the current TV channel.
3. With Frame 1 still selected, add this script just below the end of the last function
definition:
4.
5. function changeTheChannel(newChannel:Number) {
6.
7. if (tvPower) {
8.
9. currentChannel = newChannel;
10.
11. tv_mc.screen_mc.gotoAndStop(newChannel+1);
12.
13. remote_mc.light_mc.play();
14.
15. }
16.
17. }
18.

×