Game Development Reference
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.
Access the square anywhere
in this class by using the
The dragObject getter allows
other classes to access
3. In the document class, you can now access the
square and its properties through the dragObject getter:
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:
In the document class, you could access the object's property like this:
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: