Game Development Reference
In-Depth Information
Methods
Methods are nothing but functions that are part of the class definition. You interact
with objects of a class by calling various methods.
The method definitions follow the structure as shown:
[public|private|protected] function name(parameter:type, …):return
type {
body
}
Each method can be defined as private, public, or protected. A public method is a
method that can be accessed by the class itself or from outside of a class. The private
method is only accessible from within the class. You sometimes need a private
method to factor commonly used code into a function and when you prefer not to
expose this to other classes. We will see how we can take advantage of a protected
method when we discuss inheritance.
Following the access keyword comes the keyword function, followed by the name of
the method. The method can take multiple parameters with a specific type defined
for each. Following the parameter list comes a colon and a return type. The return
type is what kind of datatype is returned by the function; if none is returned, void
many be specified. Now let's see some examples.
An example constructor method that takes a number of parameters is as follows:
public function Monster(attackPower:Number,
maxLife:Number,
respawnRate:Number,
lifeUpdateRate:Number)
{
}
An example of a public method that accepts a few parameters and returns nothing is
as follows:
public function causeDamange(damage:Number,
avatar:Avatar):void {
}
 
Search WWH ::




Custom Search