Game Development Reference
In-Depth Information
The keyword void is used to denote a function that does not
return anything (and will cause an error if it attempts to), and
all other types that variables can use can also be used here. If
you leave off the return value altogether, you can opt to return
something or not, depending on some piece of internal logic.
However, as a best practice, a method should always declare
what it will return as it helps to catch errors and maintains
Getter and Setter Methods
There are two special types of methods you can create when you
want to expose a variable outside its class but want to control how
the variable is used. They are known as accessor
or getter and
methods, and they are called like normal variable assign-
ments but act like functions underneath. You can use them to
make read-only variables or to perform actions on a value before it
is set as a variable. There are a few rules to follow when using
these special methods: getter methods never accept any parameters
and must specify a return type and setter methods may only have
one parameter and never return anything. Let
s look at a couple of
examples in a single script.
package {
public class MyClass {
protected var _maxNameLength:int = 8;
protected var _name:String;
protected var _lives:int = 3;
public function get name():String {
return _name;
public function set name(value:String):void {
name = value.substr(0,maxNameLength);
public function get lives():int {
return _lives;
var myInstance:MyClass = new MyClass(); = " CHRISTOPHER " ;
trace(; //OUTPUTS " CHRISTOP " ;
trace(myInstance.lives); //OUTPUTS 3;
myInstance.lives = 10; //THROWS ERROR
Search Nedrilad ::

Custom Search