Game Development Reference
In-Depth Information
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:
l]_g]ca
w
eilknpbh]od*`eolh]u*Ikrea?hel7
lq^he__h]oo@n]c]^haK^fa_p
w
r]n[`n]c]^haK^fa_p6Ikrea?hel
lq^he_bqj_pekj@n]c]^haK^fa_p$]juK^fa_p6Ikrea?hel%
w
[`n]c]^haK^fa_p9]juK^fa_p7
y
++Cappan
lq^he_bqj_pekjcap`n]cK^fa_p$%6Ikrea?hel
w
napqnj[`n]c]^haK^fa_p7
y
y
y
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:
lner]par]n[omq]na6@n]c]^haK^fa_p9jas@n]c]^haK^fa_p$omq]na%7
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:
[omq]na*`n]cK^fa_p
You can access any of the object's properties like this:
[omq]na*`n]cK^fa_p*t
[omq]na*`n]cK^fa_p*se`pd
Figure 10-5 illustrates this simple example of how composition works.
 
Search Nedrilad ::




Custom Search