Custom Functions

[ LiB ]

You have the ability to define your own commands in ActionScript. You can name your command whatever you want. You can also make Flash do many things. You can even define commands in Flash to control very complicated enemies in whatever game you think of creating. The best part about the ability to create your own commands (a.k.a. functions) is that once you have your command defined, you can call it, or execute it, as many times as you want simply by using its name and a pair of parentheses.

To define your own commands, or functions, you can simply create a named block with the keyword function before it, like so:

 function myFunk() {   // Do something } 

So how will you execute such a function? It's not a handler, so it doesn't execute after a certain event; so when does it execute? When you call it, of course. But how do you call a function?

In this case, we named our function myFunk . All we have to do to execute it is shout out its name somewhere else in the project, like this:

 myFunk(); 

The previous code will execute whatever is inside myFunk . Of course, any genius would realize that there is no real code in myFunk . But, either way, Flash would go into that function and then return where it left off.

Allow me to show you a simple but real example.

 display(); function display() {   trace ("My first function!"); } 

If you were to start a new project and type this into the first frame of the first layer then test the movie, you would get "My first function!" as your output. Why? The defined function cannot execute by itself. It's merely a definition. The only executable code in that listing is the function call, display() ;.

The function call tells Flash to replace that code with the definition and execute it. What's inside the definition? The trace command, of course. This is why you see this output.

Let's play with variables for a bit. Let's say we had a local variable called temp01 ; our function won't be able to access the variable unless it is global. How can you expand the variable's scope? By not using the var keyword. This way, every function will be able to see the variable within that frame in the Movie Clip. But what if we don't want that to happen? What if we only want to reveal this variable to a certain function? Lucky for us, there is a way for only that function to access our discrete values.

In the project file GDA_PROG4.7.fla you can find the following listing:

 var temp01 = 5; var temp02 = 7; var temp03; temp03 = sum(temp01, temp02) display(temp03); outputTB = temp03; function sum(num01, num02){   return num01+num02; } function display(disp01) {   trace (disp01); } 

Towards the end of the code, you can see two function definitions. They were declared in a slightly different way than how I introduced them to you. They have arguments between their parentheses.

If you look at the SWF file, you will see that the output is 12. The reason for this is that I created a dynamic textbox on the stage and assigned the variable outputTB to it. This way, any value that I assign to outputTB will be immediately displayed in the textbox.

NOTE

TIP

How do you assign a variable to a textbox? All you have to do is create a textbox and access the Properties panel. Make sure it's a dynamic textbox from the drop down on the left. After you have set this setting, you will have a textbox labeled Var: towards the right.This is where you type in the name of the variable by which you want your textbox to be controlled.

After you understand the file's setup, you can start breaking down this program. You are already familiar with creating variables, so let's skim the first three lines. I created three new variables named temp01, temp02 , and temp03 . temp01 and temp02 I assigned 5 and 7, respectively. temp03 was left un- initialized because we're going to assign a value later, for display purposes.

The main body of the program is as follows :

 temp03 = sum(temp01, temp02) display(temp03); outputTB = temp03; 

The first line adds our first two variables. It also assigns the result to temp03 . The second line then calls another self-defined function that displays the value. And finally, as I told you before, the textbox is also outputting the value because we are assigning temp03 to the variable that was assigned to the textbox.

Check out the sum function and allow me to explain its new style of definition.

 function sum(num01, num02){   return num01+num02; } 

Here we have the usual function keyword. After that is our aliasin this case I named it sum . There are also two arguments within the parentheses, separated by a comma. These are dummy variables that represent real variables that will be passed along to the function. As our original variables are local, they cannot be seen by this function. The only way their values can be utilized is by passing on the values through these arguments, as you saw up above.

To avoid creating a new dummy variable to store the result inside the sum function, I embedded the addition operator in the same line as the new keyword, return . This keyword returns a value to wherever the function was called, allowing me to use the alias sum as the assigning variable. This is what allowed me to store the result in the temp03 variable.

 function display(disp01) {   trace (disp01); } 

Now that you've seen that complicated pound cake, let's check out an easier definition. I listed our display function above. Its definition is simple because it only has one argument and because it does not operate on the value except for passing it on to the trace command.

Again, disp01 is only a dummy variable and it only serves the definition. The real variable is passed on from the main code or from another function.

NOTE

TIP

Try creating your own set of func tions just for practice.Try creating subtracting, multiplying, dividing, and other types of functions. How about one that finds the power of a num ber? Be careful with how you name your functions. If the name is already a keyword, you might get an error. A good way of checking that the alias is not taken is by checking on the syntax highlighting. If it doesn't color to the keyword color, then your name is good.

[ LiB ]


Game Development with ActionScript
Game Development with ActionScript
ISBN: 1592001106
EAN: 2147483647
Year: 2004
Pages: 162
Authors: Lewis Moronta

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net