Game Development Reference
In-Depth Information
Next, we need to modify our constructor to create and initialize our user input object.
To accomplish this, we simply add the following line of code to the end of our con-
structor, just above the closing } :
m_UserInput = new UserInput();
It is a good idea to add some member methods to our UserInput class to make
handling user input a bit simpler for us. So, let's create a new method named
IsKeyPressed() , which looks like the following code:
public bool IsKeyPressed(Key key)
{
return m_KeyboardStateCurrent.IsPressed(key);
}
This method checks if the specified key is pressed, and returns true if it is or false
if it is not. As you can see from the code in this method, the KeyboardState object
has the IsPressed() method that we use to see if the specified key is pressed. It
also has an IsReleased() method for testing if a key is not pressed. In addition to
these, it has PressedKeys and ReleasedKeys properties that return a list of the
currently pressed keys and currently not pressed keys respectively. And lastly, it has
the AllKeys property that gives you the states of all keys on the keyboard.
Note
The downloadable code for this chapter contains some additional keyboard
handling methods like this one. They are IsKeyReleased() and
IsKeyHeldDown() .
There is now just one step left before we can see our keyboard input code in action.
We need to add some code into our UpdateScene() method to check for some key
presses. Here is the new code in the UpdateScene() method:
Search WWH ::




Custom Search