Game Development Reference
Composition allows many objects to share the same class. The great thing is that you don't need to
hard-code the class you want them to use into the symbol's properties. You can decide when you
write the program which object should use which class, and even switch the class it uses at any time
you like. Composition requires a bit more code and a bit more structural planning than inheritance,
but ultimately gives you a greater flexibility when you build your games.
Let's look at a very simple example. You could create a very basic class called @n]c]^haK^fa_p that
accepts one parameter: the name of any Movie Clip instance that wants to use the class. The class
might look like this:
Imagine that you have an object on the stage called omq]na that you want to make dragable. In your
I]ej document class, you then create the dragable [omq]na object like this:
In the I]ej document class you can then access the actual object you want to drag through the
@n]c]^haK^fa_p class's getter, like this:
You can access any of the object's properties like this:
Figure 10-5 illustrates this simple example of how composition works.