Game Development Reference
In-Depth Information
Properties are nothing but variables that belong to the class and that all methods
have access to. Another way to look at it is that the variables (also called local
variables) are only accessible within the method they are defined in, while the class
properties are accessible to the entire class.
You may fine-tune the access of the properties even beyond the class it is defined in.
Here is a general syntax to define a property:
[public|private|protected] var name:type;
The declaration starts with either the public , private , or protected keyword
followed by the var keyword, which is short for variable, followed by a name of
your choice and ending with the type for the variable.
Here is an example that illustrates the class property declaration and its usage. We
will take the previous example of variables and turn them into class properties.
package {
import flash.display.Sprite;
import world.Avatar;
public class HelloWorld extends Sprite
{
private var anInteger:int = 3;
private var aNumber:Number = 3.1415;
private var aString:String = "This is a string."
private var yesOrNo:Boolean = false;
public function HelloWorld()
{
trace("Value of anInteger: " + anInteger);
trace("Value of aNumber: " + aNumber);
trace("Value of aString: " + aString);
trace("Value of yesOrNo: " + yesOrNo);
}
}
}
Running this sample in debug mode would produce the exact same results as the
previous version.
When do you decide that something should be a method variable or a class
property? The answer is easy. It simply depends on how you want to use it, and if
you need access to a variable that needs to be accessed by multiple methods and
need to save its value over the life of the object. In this case, it is a good idea for it to
be a class property. An example would be x and y coordinates in a display class.
 
Search WWH ::




Custom Search