# ADDING PARAMETERS TO FUNCTIONS

 In the previous exercise you learned how to create a function and call it from any timeline. In this exercise, you'll add parameters to a function and learn how to use them. The following is the syntax for creating a function that accepts parameters: ` function convertToMoonWeight (myWeight){ weightOnMoon = myWeight/6.04; } ` The sole parameter for this function is myWeight which is also used within the function definition, just as if it were a preexisting variable. The following is an example of the syntax used to call this function: ` convertToMoonWeight(165); ` When calling this function, a value is passed in (that is, placed within the parenthesis). In this case, that value is a person's weight here on Earth (165). This value (165) sets the value of the myWeight parameter throughout the function before it executes. 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. TIP When a function is called, a temporary array called arguments is created. This array contains all parameters passed in 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","Hayes"); ` In this example, the following would 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: Hayes Accessing the argument array can enable 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 7, Using Dynamic Data. In this exercise, you'll add button functionality to the numeric keypad on the remote control as well as to the TV channel Up and Down buttons. The numeric buttons will work by calling a function and passing in the number of the channel to jump to. You will also modify the togglePower() function slightly. Open television2.fla in the Lesson05/Assets folder. This file is set up in similar fashion to the one we just worked on, with the exception that the screen movie clip instance contains frames that represent six channels rather than a single channel. Select Frame 1 of the Actions layer on the main timeline and open the Actions panel. Add this ActionScript to that frame at the end of the previous function definition: ` function changeTheChannel (newChannel) { if (tvPower) { currentChannel = newChannel; tv.screen.gotoAndStop(newChannel + 1); remote.light.play(); } } ` You have just created a function that accepts a parameter. This function will change the TV channel based on the parameter (newChannel ) received. All of the actions that the function will perform are enclosed in an if statement, which is used to allow channels to be changed only if tvPower is true . The function then proceeds to set a variable used to store the current channel of the television to the value of the parameter value sent to the function. The next two lines should be familiar from the togglePower() function we discussed in the previous exercise: They set the frame in the screen movie clip instance (causing the television to change channels) and instruct the light on the remote control to blink. To understand this, consider a simple example. Assume the following function call is made to this function: ` changeTheChannel(4); ` The function would ask whether tvPower is true (TV is on) before doing anything else. If it is, currentChannel is given a value of 4 (same as the parameter passed to the function). Next, the screen movie clip instance is moved to a frame based on the value of the parameter value passed to the function, plus one (or 4 + 1). Thus, the screen instance is moved to Frame 5. Your newly created function is now ready for use. Next, we'll add function calls to the numeric buttons on our remote control. Double-click the remote movie clip instance on the main timeline to edit it in place. With the Actions panel open, select the round button under the number 1 on the remote keypad and add this ActionScript: ` on (release) { _root.changeTheChannel(1); } ` Repeat this step for each of the round buttons under the numbers 2 through 6. In the parameter area (in the parenthesis) of each function call, enter the channel number that the television should display for each of the buttons. The changeTheChannel() function resides on the main timeline so you must call it using the path to its location: _root . Choose Control > Test Movie. Press the Power button on the remote control to turn on the TV, then use the numeric keypad on the remote control to change the channels. If you press the channel buttons before turning on the television, the changeTheChannel() function will not perform the change request. If the television is on and you press one of the channel buttons, that button number is passed into the changeTheChannel() function and the screen movie clip instance will move to the correct frame (channel). Close the testing movie to return to the authoring environment. With the Actions panel open, select Frame 1 of the Actions layer. This frame contains two function definitions, one for turning the TV on and off and another for changing channels using the numeric buttons on the remote-control keypad. However, you'll notice some redundancy between these functions: Both are set up so that when either is called, they tell the remote-control light to blink as well as send the screen movie clip instance to the correct frame. Since it's best to fix this type of redundancy whenever possible, let's correct this problem. With the Actions panel still open, modify the togglePower() function to read as follows: ` function togglePower () { if (tvPower) { changeTheChannel(0); tvPower = false; } else { tvPower = true; changeTheChannel(1); } } ` This function now makes use of the changeTheChannel() function to change the channel when the power is turned on or off. When the TV is turned on, the togglePower() function now makes a call to the changeTheChannel() function and passes in the number 1. This means that every time the TV is turned on, it will start on Channel 1. When it is turned off, it goes to Channel 0 (the off state of the TV screen). This demonstrates how one function can contain a call to another. NOTE You'll notice that in the first part of the if statement above, the call to the changeTheChannel() function happens before tvPower is set to false . This is because we defined the changeTheChannel() function so that it only works if tvPower is true (which it is if this part of the statement is executed). If tvPower were set to false first, the function call of changeTheChannel(0) would do nothing. The else part of the statement works just the opposite: The value of tvPower is set to true first, before the function call. Once again, this is because it has to have a value of true before the function call will have an effect. Now let's create a couple of functions that will allow us to increment and decrement channels using the Up and Down arrows on the remote. Select the first frame of the Actions layer on the main timeline and open the Actions panel. Insert the following line of ActionScript in this frame, just below where it says tvPower = false; : ` numberOfChannels = 6; ` This line of code creates a variable called numberOfChannels and assigns it a value of 6. This variable will be used in a function (which we'll create in a moment) that will be used to increment channels each time it's called. You'll remember that the screen movie clip instance only contains graphics representing six channels; thus, this value of 6 represents the total number of channels our TV can display and will be used to prevent the incrementing of channels beyond Channel 6. Let's see how this will work. Add the following ActionScript at the end of the currently selected frame: ` function channelUp () { if (currentChannel + 1 <= numberOfChannels) { changeTheChannel(currentChannel + 1); } } ` This function which does not accept parameters bumps up the channel by 1 each time it's called. However, it uses a "safety mechanism" to prevent going beyond Channel 6. Recall that a variable named currentChannel is set every time the changeTheChannel() function is called (see Step 2). The value of this variable represents the channel currently displayed minus 1. Thus, if Channel 4 is currently displayed, this variable's value will be 3. Before the channelUp() function will execute, it uses an if statement to determine whether the current channel incremented up (the value of currentChannel + 1) will still be less than or equal to the upper channel limit (the value of numberOfChannels , or 6). If the condition is satisfied, the changeTheChannel() function is called with a parameter that has a value of the current channel plus 1. This will cause the next channel to be displayed. This if statement contains no accompanying else statement. Thus, if the condition is not satisfied, this means that Channel 6 is currently displayed and no further actions are performed. Add the following ActionScript at the end of the currently selected frame: ` function channelDown () { if (currentChannel 1 >= 1) { changeTheChannel(currentChannel 1); } } ` Like the channelUp() function, this function does not accept parameters. When called, it determines whether the value of the currentChannel variable decremented by 1 is greater than the lower bound of 1, thus preventing the user from "channeling down" beyond Channel 1. If this condition is satisfied, the changeTheChannel() function is called and passed the value of the currentChannel minus 1. This will cause the previous channel to be displayed. As with the if statement in the channelUp() function definition, this if statement contains no accompanying else statement. Thus, if the condition is not satisfied, this means that Channel 1 is currently displayed and no further actions will be performed. Now it's time to add function calls to the Up and Down buttons on our remote control that will use the channelUp() and channelDown() functions we just created. Double-click the remote movie clip instance on the main timeline to edit it in place. With the Actions panel open, select the Up arrow button and add this ActionScript: ` on (release) { _root.channelUp(); } ` Every time this button is clicked, the channelUp() function in the root will be called. When the current channel reaches its upper limit (as defined by the numberOfChannels variable), the channels will no longer increase. With the Actions panel open, select the Down arrow button and add this ActionScript: ` on (release) { _root.channelDown(); } ` Like the Up button, this action calls a function that resides in the root. Every time this button is pressed, channelDown() is called andthe currentChannel variable is decremented (as long as it's greater than the lower limit). The television is then set to the correct channel. Choose Control > Test Movie, then turn the television on using the Power button and use the Up and Down buttons to change channels. Notice that you can select any channel, and from there use the Up and Down buttons to change the channels. Using a variable that stores the current channel and functions as you have done here makes this type of functionality simple. Close the test movie and save your work as television3.fla. You have now created a functional remote control for a television. In the next exercise, you'll learn how to use functions in a new way while adding a cable box display.

Macromedia Flash MX ActionScripting: Advanced Training from the Source
ISBN: 0201770229
EAN: 2147483647
Year: 2002
Pages: 161