Game Development Reference
In-Depth Information
Fig. 23.1
The sprite
containing the four arrows,
each in a different direction
then darker. Since the
Arrow
class stores multiple sprites, it also inherits from the
GameObjectList
class. In the constructor, we load the two arrow images. We set the
visibility of the hover sprite as well as the 'pressed' status to false:
arrow_normal =
new
SpriteGameObject(assetname_normal, 0, id, sheetIndex);
arrow_hover =
new
SpriteGameObject(assetname_hover, 1, id, sheetIndex);
this
.Add(arrow_normal);
this
.Add(arrow_hover);
arrow_hover.Visible =
false
;
pressed =
false
;
The sheet index which is passed as a parameter to the
Arrow
constructor is passed
along to the actual sprites, so that the correct arrow direction is selected.
Inside the
HandleInput
method, we check if the hover sprite should be visible by
calculating if the mouse position is inside the bounding box of the arrow sprite. The
arrow is marked as 'pressed' if the hover sprite is visible and the left mouse button
has been clicked:
arrow_hover.Visible = arrow_normal.BoundingBox.Contains(
(
int
)inputHelper.MousePosition.X,
(
int
)inputHelper.MousePosition.Y);
pressed = inputHelper.MouseLeftButtonPressed() && arrow_hover.Visible;
Finally, we add a few convenient properties to the
Arrow
class, so that we can check
what the width and height of the object is, and whether it has been pressed.
23.2.2 The Animal Selector
The animal selector uses the
Arrow
class to display four arrows when the player clicks
on an animal (see Fig.
23.2
). These four arrows are stored as member variables in
the
AnimalSelector
class. Furthermore, since the selector controls a particular animal,
we have to keep track of which one it controls. Therefore, we also store a member
variable containing a reference to the target animal:
protected
Arrow arrowright, arrowup, arrowleft, arrowdown;
protected
Animal selectedAnimal;
In the constructor method, we then create the four
Arrow
objects, and position them
appropriately (for the full code, see the
PenguinPairs5
project belonging to this chap-