Flash CS3 Documentation |
|||
| Learning ActionScript 2.0 in Adobe Flash > Functions and Methods > About functions and methods > About types of methods and functions | |||
Functions that belong to a class are called the methods of that class. There are several types of functions that you can use in your applications, including built-in functions, named and user-defined functions, anonymous functions, callback functions, constructor functions, and function literals. The following sections contain information on how to define these functions.
You can also write functions in an ActionScript class file. You use these functions as methods in your scripts. In the following example, the Person class displays a constructor method, class methods, instance methods, and accessor methods (getters and setters). The comments in this code sample show where these methods occur in the code.
|
NOTE |
|
For information on writing class files, such as the following, see Classes. |
class Person {
public static var numPeople:Number = 0;
// instance members
private var _speed:Number;
// constructor
public function Person(speed:Number) {
Person.numPeople++;
this._speed = speed;
}
// static methods
public static function getPeople():Number {
return Person.numPeople;
}
// instance methods
public function walk(speed:Number):Void {
this._speed = speed;
}
public function run():Void {
this._speed *= 2;
}
public function rest():Void {
this._speed = 0;
}
// getters/setters (accessor methods)
public function get speed():Number {
return this._speed;
}
}
For a full demonstration of how to write methods like the ones in the previous code sample, see Classes. The methods that you use in your code might belong to a class that is built into the ActionScript language. MovieClip and Math are examples of top-level classes that you might use in an application. When you use methods from these classes in your code, they are functions written in the built-in class (similar to the previous code sample). Alternatively, you could use methods from a custom class that you wrote yourself.
Functions that don't belong to a class are called top-level functions (sometimes called predefined or built-in functions), meaning that you can call them without a constructor. Examples of functions that are built in to the top level of the ActionScript language are trace() and setInterval().
To add a top-level function call to your code, just add a single line of code in the Script pane of the Actions panel. For example, type the following:
trace("my message");
When you test the SWF file with this single line of code, the top-level trace() function is called, and text appears in the Output panel.
Remember: when you want to assign a method to a property, you omit the parentheses after the method name because you're passing a reference to the function:
my_mc.myMethod = aFunction;
However, when you want to invoke a method in your code, you need to include the parentheses following the method name:
my_mc.myMethod();
|
NOTE |
|
For more information on top-level functions, see About built-in and top-level functions. |
You can also define functions in numerous other ways. For more information on each kind of function, see the following sections:
For information on writing and using functions and methods, see the following related sections. For information on using functions, see Using functions in Flash. For information on using methods, see Understanding methods.
|
NOTE |
|
For information on writing code using Script Assist, see Using Flash. |
Flash CS3