Game Development Reference
In-Depth Information
In this example, the class being extended is Rectangle, which
has no ties to the EventDispatcher hierarchy. By implementing the
IEventDispatcher interface and creating an instance of the Event-
Dispatcher class, we can enjoy both the functionality of a Rectangle
and an EventDispatcher. When the width or height of this special
rectangle changes, it will dispatch an event to anything that is
listening. We will cover more on events in an upcoming section.
So, the question now is probably
When should I use
interfaces?
Unlike some OOP proponents who believe the answer
is
I believe it really depends on the breadth of the game
or application you are building. Sometimes, in quick games where
I am the sole developer, I prefer inheritance because I usually have
the luxury of defining my entire inheritance chain and I don
always,
thave
to work within a preexisting framework. I find interfaces to be most
helpful when working with other developers (particularly those at
other companies where we
'
re not eager to share specific code with
each other) because we can agree upon an interface for our
common class elements and integration of our respective compo-
nents is far more likely to work without a hitch as a result.
Interfaces are also extremely useful in creating flexible, reusable
game engines for more complex games, as we will see in later
chapters. In the end, interfaces are just a tool, and like any tool, it
should be used when called for and left alone the rest of the time.
In fact, in the mobile examples, we
'
ll look at toward the end of this
book, where performance is a key factor, and interfaces are often
not the answer.
'
Linking Classes to Assets in Flash
A common staple of my game development (and arguably one of the
biggest advantages of developing games in Flash) is the ease with
which you can link a Flash class to an item in your FLA library. Any
item in your library can have an associated class linked to it, but the
ones you will probably use the most are the DisplayObject sub-
classes Sprite and MovieClip. First, how Flash creates classes for
library items should be understood.
If you set the linkage property of a symbol in the library, it has a
class created for it when the SWF is compiled, regardless of whether
or not one was explicitly defined. For instance, take a Sprite in an
FLA library named
withasimplebluesquareinsideit.
Because the symbol is not a Sprite directly but rather an extension
of Sprite, a new class with the name
square,
will be created at
compile time that extends Sprite and looks like the following:
square
package {
import flash.display.Sprite;
public class square extends Sprite {}
}
Search Nedrilad ::




Custom Search