Game Development Reference
In-Depth Information
in our example consists of a single image that we have to load and display. We create
a
SpriteGameObject
instance for that, which we store in a member variable
title
and
we add it to the game world:
title =
new
SpriteGameObject("spr_title", 100);
this
.Add(title);
We choose a high value for the layer (100) so that we can be sure that the title is
drawn on top of everything. But we have to do a little extra work to properly handle
input and update the game world, since we want the game to start only once the
title screen is no longer visible. We can do that by adding a few instructions to the
HandleInput
method to distinguish between two states: the state in which we show
the title screen and the state in which we are playing the game:
if
(title.Visible)
{
if
(inputHelper.KeyPressed(Keys.Space))
title.Visible =
false
;
}
else
base
.HandleInput(inputHelper);
Looking at the
if
instruction, you can see that if the title screen is visible, we only
react when the player presses space. In that case, we set the title screens visibility
flag to
false
so that it is not drawn anymore. If the title screen is not visible, we call
the
HandleInput
method on all the game objects in the game world, in other words,
the game will react to the player as it should.
We follow very much the same procedure for the
Update
method, where we only
update the game world if the title is not visible:
if
(!title.Visible)
base
.Update(gameTime);
When we start the game, the player now gets to see a title screen before the game
starts. We are not done yet. In the next section, we are going to add a simple button
GUI element for showing a help frame.
17.3 Adding a Button for Showing the Help Frame
In this section, we will explain how you can add a simple button to a game, which
we will use to display a help frame. In order to do that, we are going to add another
class,
Button
, to our program. We will inherit from the
SpriteGameObject
class, and
add some simple behavior for checking if the player pressed a button or not. In the
Button
class, we declare a boolean member variable that indicates whether the button