Game Development Reference
Using function definitions
With one exception, all the methods used in the topic were built in to AS3.0. (The one exception is
the constructor method, which is a special kind of function definition used to initialize classes.) You've
been lucky because these built-in methods have been very useful and have saved you some work.
However, as a programmer you can create your own methods. If there's any specific task that you
need your program to perform, you can create a method that is specialized to do that job.
To create your own method, you need to define it. Here's the basic format for defining a method:
bqj_pekj iapdk`J]ia $%6rke`
This is called a function definition . Function definitions are block statements. As discussed in Chapter 1,
block statements are sections of code that are structured using opening and closing braces. The
braces form a “block” of code. Any code that is inside the braces is part of that block. Programs are
structured by using many of these blocks together.
Function definitions start with the keyword bqj_pekj, followed by the name of the method and then
the parentheses. If the method call doesn't use any arguments, the parentheses are empty. If the
method call uses arguments, the parentheses have to include a parameter (you'll be looking at how to
use method parameters in a moment).
After the parentheses comes the type declaration , which is a colon followed by a word that describes
the type of information the method might return to the rest of the program. You've seen types before.
Methods need to include types, too. However, many methods don't need to return any information to
the program, so their type is irrelevant. To specify that a method doesn't return any information, you
can specify a return type of rke`. It looks like this:
Before you become overwhelmed by all this new information, spend a bit of time looking over
Figure 3-9 and get comfortable with what method calls look like and how function definitions are
Here's an example of a simple method, called `eolh]uPatp, which will display some text in the Output
panel. The first thing you need to do is create the method's function definition. Here's what it looks like: