Game Development Reference
In-Depth Information
using
Microsoft.Xna.Framework;
1
2
3
class
Button : SpriteGameObject
{
4
protected bool
pressed;
5
6
7
public
Button(
string
imageAsset,
int
layer = 0,
string
id = "")
:
base
(imageAsset, layer, id)
8
{
9
pressed =
false
;
10
}
11
12
13
public override void
HandleInput(InputHelper inputHelper)
{
14
Rectangle rect =
new
Rectangle((
int
)GlobalPosition.X, (
int
)GlobalPosition.Y,
15
sprite.Width, sprite.Height);
16
pressed = inputHelper.MouseLeftButtonPressed() &&
17
rect.Contains((
int
)inputHelper.MousePosition.X,
18
(
int
)inputHelper.MousePosition.Y);
19
}
20
21
22
public bool
Pressed
{
23
get
{
return
pressed; }
24
}
25
}
26
Listing 17.1
A simple class for representing a button
When the help frame is visible, we want to be able to remove it by pressing the
space bar. So, our final
if
-instruction is slightly more complicated:
if
(helpButton.Pressed || (helpFrame.Visible && inputHelper.KeyPressed(Keys.Space)))
helpFrame.Visible = !helpFrame.Visible;
Finally, we have to make sure that the game is not updated when the help frame is
displayed. We can do that in the
Update
method by only updating the game objects
if the help frame is not visible:
if
(!helpFrame.Visible && !title.Visible)
base
.Update(gameTime);