Game Development Reference
In-Depth Information
You will see that the Flash Builder creates a new file called Avatar.as under the
world folder, and it's even nice enough to get us started with the following:
package world
{
public class Avatar
{
public function Avatar()
{
}
}
}
Notice that since the class is defined under the world folder, the package name was
automatically set to world . The class name is Avatar and it is defined as a public
class, which means that this class may be referenced by other classes in
other packages.
It also creates a method with the same name as the class. This is a special method
called the constructor, which means if you want to create a new object of this
class, you do so via this method. Also notice that there is no return value for the
constructor; it always returns a newly created object of class Avatar.
Now, how do we create an instance or an object of class Avatar? We will modify
the main class HelloAvatar as follows:
package {
import flash.display.Sprite;
import world.Avatar;
public class HelloWorld extends Sprite
{
public function HelloWorld()
{
var anAvatar:Avatar;
anAvatar = new Avatar();
}
}
}
You will need to first import the class with an import statement, so the compiler
knows exactly what you mean by an Avatar. You then create a variable that points to
and gives it a name, in this case, anAvatar . Finally, you create a new object that is an
instance of Avatar using the new operator followed by the constructor.
There, you are now in the object-oriented world!
 
Search WWH ::




Custom Search