mort8088 Just another WordPress site

6Mar/11Off

XNA 4.0 – Tutorial 3 – Input from Keyboard.


Originally posted 2009 - Drawing a single sprite on screen with a variable position, and then moving it with the power of your mind, fingers and keyboard. I've updated things to reflect using VS-2010 and XNA 4.0

A game is only a game if you get to do stuff in it, and for that we're going to need some way to give our input to the game, and with that affect change within the world. We're going to update our last project so that we can move the sprite around the screen. Start by opening the project from "XNA 4.0 - Tutorial 2 - Sprites" It'll give us the code base to start.

Add some global variables at the top of game1.cs to hold the keyboard state and sprite position :-

This gives us two places to store the keyboard state, one for the current cycle and one for the last cycle, that way we can compare the two values and see if a key was pressed since the last update or if it has been held down since the last update. The variable that will hold the sprite position is a structure that just contains the X & Y properties, as opposed to the KeyboardState class that contains both properties and methods.

Next go to Initialize() and add these lines before the base.Initialize();, they set-up everything ready for us:-

Then change the call to spriteBatch.Draw in Draw()to use the position variable:-

Finally change the update() method and add these lines:-

The first line puts the currentKeyboardState from the last pass through into the oldKeyboardState. The second line gets the current keyboard state.

For my first example we're going to be checking for the arrow keys, to do this we need to add code to the update method that will check for a key being down, we don't care when it was pressed just is it being pressed now. Luckily there's a method for what we want to do bool IsKeyDown(Keys key) we'll check each of the arrow keys in turn and update another Vector2 and use that to update the sprites position.

First add a local Vector2 that we can set in relation to what keys we are pressing:-

Next we'll add four if(...) statements, that will check each key in turn and update Delta accordingly:-

Remember the screen origin(0,0) is in the top left corner so to move up we subtract -1 from the Y axis.

Now we take Delta and update the sprite position:-

If you press F5 when the game window appears pressing the arrow keys will move the sprite by your command.

Another example find the lines that read:-

These are checking the GamePads back button to see if you want to Exit the game when you press back it will Start the Exit process for the game. We want to do that on you releasing the Esc key that is to say don't Exit when you press the key only when you let go:-

As you can see we're checking that the oldKeyboardState shows the key down and the new currentKeyboardState shows that it is up, so it was down but now it's up and then we'll quit.

Conclusion

Well you can see it's going to take alot of lines of code to check each of the keys on a full QWERTY keyboard, we'll have to look into that at some point.

Comments (0) Trackbacks (0)

Sorry, the comment form is closed at this time.

Trackbacks are disabled.