Game Development Reference
In-Depth Information
Chapter 6
Reacting to Player Input
6.1 Introduction
In this chapter, we will show you how your game program can react to mouse clicks
and button presses. In order to do this, we need a instruction called if that executes an
instruction (or a group of instructions) if a condition is met. We will also introduce
enumerated types as another kind of primitive type.
6.2 Reacting to a Mouse Click
6.2.1 ButtonState : An Enumerated Type
In the previous examples, we have used the current mouse state to retrieve the mouse
position. However, a MouseState object contains a lot of other information as well.
For example, it can be used to find out whether a mouse button is pressed or not.
For this, we can use the properties LeftButton , MiddleButton and RightButton . What
these properties give is a value of type ButtonState . So we could save this value as
follows:
ButtonState left = currentMouseState.LeftButton;
Primitive types— C# makes a distinction between the more complicated
types representing a class and the very basic types representing things like
integers or enumerations. The latter types are also called primitive types , be-
cause they form the building blocks of the more complicated class types .
You might guess that ButtonState is a class, however, it is actually an enumerated
type . An enumerated type is very similar to the integer type, with the difference that
 
Search WWH ::




Custom Search