Game Development Reference
In-Depth Information
24.3 Finishing the User Interface
24.3.1 Showing Hints
Now that we have reorganized our code, there are still a couple of features we would
like to add to the Penguin Pairs game. As a first step, we want to be able to show a hint
when the user clicks on a button. The hint consists of an orange arrow that is visible
for a second. When we loaded the level, we read the hint position and direction from
the file:
stringlist = reader.ReadLine().Split();
int hintx = int .Parse(stringlist[0])
1;
1;
int hintdirection = int .Parse(stringlist[2]);
int hinty = int .Parse(stringlist[1])
We then create a SpriteGameObject instance to load the arrow, select the correct sheet
index, and position it appropriately before we add it to the game world.
SpriteGameObject hint = new SpriteGameObject("Sprites/spr_arrow_hint@4", 2,
"hint", hintdirection);
new Vector2(73, 72);
hint.Position = new Vector2(hintx, hinty)
playingField.Add(hint);
In order to temporarily display the arrow, we reuse the VisibilityTimer class from the
Jewel Jam game. We create an instance of this class, and add it to the game world as
well:
VisibilityTimer hintVisible = new VisibilityTimer(hint, 0, "hintVisible");
playingField.Add(hintVisible);
We also add a button that the player can click to display the hint on the screen:
hintButton = new Button("Sprites/spr_button_hint", 1, "hintButton");
hintButton.Position = new Vector2(916,20);
this .Add(hintButton);
Finally, we extend the HandleInput method to deal with the hint button being pressed:
if (hintButton.Pressed && hintButton.Visible)
{
VisibilityTimer hintVisible = this .Find("hintVisible") as VisibilityTimer;
hintVisible.StartVisible();
}
Why do we check if the hint button is visible? The reason for that is that in some
cases, the hint button is no longer visible:
Search WWH ::




Custom Search