Game Development Reference
In-Depth Information
1. The stage contains an object called square.
2. Send the DragableObject class a reference to
the square object. Assign the new dragable object
that it creates to a private property called _square.
Document class
lner]par]n[omq]na6@n]c]^haK^fa_p9jas@n]c]^haK^fa_p$omq]na%7
DragableObject class
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
omq]na
Access the square anywhere
in this class by using the
_dragableObject property.
The dragObject getter allows
other classes to access
this object.
3. In the document class, you can now access the
square and its properties through the dragObject getter:
Document class
[omq]na*`n]cK^fa_p
[omq]na*`n]cK^fa_p*t
[omq]na*`n]cK^fa_p*se`pd
Figure 10-5. Composition allows any object to acquire the properties and methods of any other class.
You could make your code a little more readable by adding another getter in the @n]c]^haK^fa_p
class that returns any of the dragable object's properties, such as the t property:
lq^he_bqj_pekjcapt$%6Jqi^an
w
napqnj[`n]c]^haK^fa_p*t7
y
In the document class, you could access the object's property like this:
[omq]na*t
This is a bit more code to write in the @n]c]^haK^fa_p class, but it makes your code much more read-
able because it mimics the way you would normally access an object's properties. If your objects need
a reference to the op]ca (to set stage boundaries, for example), you need to pass the @n]c]^haK^fa_p
class a reference to the op]ca object as one of its parameters when you instantiate the object. You
need to do this because the @n]c]^haK^fa_p class itself is never added to the stage, so it has no other
way to access the stage's properties. To do this, you can change the @n]c]^haK^fa_p constructor
method so that it looks like this:
lq^he_bqj_pekj@n]c]^haK^fa_p$]juK^fa_p6Ikrea?hel(op]ca6K^fa_p%
w
 
Search Nedrilad ::




Custom Search