Game Development Reference
In-Depth Information
Constructors
Every class has a constructor, even if it does nothing and is not
explicitly defined. It is the function, with the same name as the
class, which is called when a new instance of the class is created.
In the case of our last example, even if we leave it out, Flash adds
the following to the class:
package flash.display {
public class MovieClip {
public function MovieClip() {
}
}
}
The constructor allows us to run any initialization code that the
new instance might need, or it can do nothing, depending on how
your class is to be used.
Constants, Variables, and Methods
A class without any data or functionality inside it is not of very
much use, so we can define variables or properties, of the class
that will store information, and methods, or functions that will
perform actions. I
m going to assume you already know how to
use variables and methods, from either earlier versions of Action-
Script or another language. Constants are entirely new to AS3 but
are not a complicated concept. Essentially, they are variables that
can only be assigned a value once. When you declare a constant
orvariable,itisbesttogiveita type , which tells Flash which
class to use as the blueprint for that variable. Below are few
examples:
'
const myInt:int = -3; //WILL ALWAYS BE -3 AND CANNOT BE MODIFIED
var myBoolean:Boolean = true;
var myString:String = " Hello World " ;
var myObject:Object = new Object();
Giving a variable a type also saves memory because Flash
knows the maximum amount of memory it needs to store an
instance of a specific class. If you don
t type a variable, as in the
following example, Flash must reserve a larger amount of memory
to accommodate any possible value.
'
var myMystery:* = " ? " ;
Once you assign a value to an untyped variable, it becomes
typed from then on, so attempts to change its type (like you could
in earlier versions of ActionScript) will result in runtime errors,
such as the following example.
Search Nedrilad ::




Custom Search