Keyboard Input

3 12 2009

Hi today I bring an issue that I watched last week about keyboard input in XNA.
It is an interesting topic, I like it to much, but it was a little complicated.
I use an example from learining XNA Book.
Keyboard input is handled via the Keyboard class, Which Is In The Microsoft.XNA.
Framework.Input namespace.

Add these class-level variables at the top of your class:

Vector2 ringsPosition = Vector2.Zero;
const float ringsSpeed = 6;

So, go ahead and add the following code to the end of your Update method, just before the call to base.Update:

KeyboardState keyboardState = Keyboard.GetState ();
if (keyboardState.IsKeyDown (Keys.Left))
ringsPosition.X -= ringsSpeed;
if (keyboardState.IsKeyDown (Keys.Right))
ringsPosition.X + = ringsSpeed;
if (keyboardState.IsKeyDown (Keys.Up))
ringsPosition.Y -= ringsSpeed;
if (keyboardState.IsKeyDown (Keys.Down))
ringsPosition.Y + = ringsSpeed;

Regards

Advertisement

Actions

Information

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Connecting to %s




Follow

Get every new post delivered to your Inbox.