Game Development Reference
In-Depth Information
Classes—defining game objects
Encapsulation is a big object-oriented word! But what exactly is it? As the word
suggests, software is best designed when you have reusable components whose
functionality is well defined and exposes a bunch of interface for its use by other
components. Further, you don't need to care much for how its functionality is
implemented. Classes are a way to achieve encapsulation in object-oriented
programming. Take for example an Array class. You don't care how the elements
are actually stored in it as long as you can tell the class that you want to add, retrieve,
delete items in the array.
A class exposes it's functionality via a bunch of methods, also called the interface for
the class. A method is simply a function that can take parameters, does something
useful, and optionally return a value. The difference between a function and the
method in this case is that the method is attached to the class. You can only refer to
the method as part of the class and not independently of the class. A class can store
its internal state in what are called properties, for example, an internal property to
keep track of the current number of elements in the array.
A class may define as many properties and methods as required, and each property
and method may also be marked as private, public, or protected. A private property
or method can be referenced only from within the class as opposed to the public
properties that may be accessed from any other class, which has access to the class.
The class itself may be restricted by specifying whether it is private or public.
A private class is only available to another class in its own package.
Creating game objects
Simply put, an object is an instance of a class. You can define an array class. This only
means that you have specified the blue print for this class, but during runtime you
need to create an instance that is usable, and you are free to create as many instances
of the same array class, for example. Further, only after you have created an instance
can you call the methods or use the properties defined on them.
Let's see how all this looks in actual code. Let us create a new project and call it
HelloAvatar and create a class of our own. Once the project is created for us, we can
get organized and create a folder under which we will stuff our class. Go ahead and
right-click on the project icon HelloAvatar and choose Folder under the New menu
item. In the dialog window that comes up, enter the word world for the
Folder name .
Right-click on the newly created world folder and choose ActionScript Class under
New menu item. Type Avatar for the Name field and click on Finish .
 
Search WWH ::




Custom Search