Log in Page Discussion History Go to the site toolbox

XNA Keyboard

From BluWiki

This page has been protected from editing by unregistered or newly registered users due to vandalism.

Introduction

This brief tutorial adds keyboard input to the example shown in the MSDN tutorial Your First Game: Microsoft XNA Game Studio in 2D.

This tutorial was created by Nathan Willingham and Seth Ringling as part of the COMP 475 XNA Tutorials.

Moving a sprite based on keyboard input

Create a new function with logic for directing the sprite based on the keyboard input, using this 4 step process:

Create a KeyboardState object

Create a KeyboardState object, containing the last known state of the keyboard (which keys are down, which keys are up)

Ex:

KeyboardState oldState;

GetState()

Call the function to retrieve the last known keyboard state, and store it in your KeyboardState object.

Ex:

KeyboardState newState = Keyboard.GetState();

Check for change

Check if the key on the keyboard you want to respond to the pressing of has been pressed since the last time you checked

Ex:

// Is the RIGHT key down?
           if (newState.IsKeyDown(Keys.Right))
           {
               // If not down last update, key has just been pressed.
               if (!oldState.IsKeyDown(Keys.Right))
               {
                   spriteSpeed.X = 50;
                   spriteSpeed.Y = 0;
               }
           }
           else if (oldState.IsKeyDown(Keys.Right))
           {
               // Key was down last update, but not down now, so
               // it has just been released.
           }

Axes

The numerical value assigned to the spriteSpeed.X and spriteSpeed.Y variables indicates the number of pixels added to the sprite's speed at each increment. The direction of the sprite is determined only by whether the values are positive, negative, or zero.

Ex: An X value of 0 and a Y value of 50 makes the sprite go right, at 50 units per increment.

For the Y value, negative numbers make the sprite go up, and positive numbers make the sprite go down. For the X value, negative numbers make the sprite go left, and positive numbers make the sprite go right.

Update your KeyboardState

Be sure to assign the keyboard's newState state to the oldState KeyboardState object at the end of the function

Ex:

newState = oldState;

Example

Alternatively, copy and paste this entire function into the existing project:

       KeyboardState oldState;
       private void UpdateInput()
       {
           
           KeyboardState newState = Keyboard.GetState();
           // Is the UP key down?
           if (newState.IsKeyDown(Keys.Up))
           {
               // If not down last update, key has just been pressed.
               if (!oldState.IsKeyDown(Keys.Up))
               {
                   spriteSpeed.X = 0;
                   spriteSpeed.Y = -50;
               }
           }
           else if (oldState.IsKeyDown(Keys.Up))
           {
               // Key was down last update, but not down now, so
               // it has just been released.
           }
           // Is the DOWN key down?
           if (newState.IsKeyDown(Keys.Down))
           {
               // If not down last update, key has just been pressed.
               if (!oldState.IsKeyDown(Keys.Down))
               {
                   spriteSpeed.X = 0;
                   spriteSpeed.Y = 50;
               }
           }
           else if (oldState.IsKeyDown(Keys.Down))
           {
               // Key was down last update, but not down now, so
               // it has just been released.
           }
           // Is the LEFT key down?
           if (newState.IsKeyDown(Keys.Left))
           {
               // If not down last update, key has just been pressed.
               if (!oldState.IsKeyDown(Keys.Left))
               {
                   spriteSpeed.X = -50;
                   spriteSpeed.Y = 0;
               }
           }
           else if (oldState.IsKeyDown(Keys.Left))
           {
               // Key was down last update, but not down now, so
               // it has just been released.
           }
           // Is the RIGHT key down?
           if (newState.IsKeyDown(Keys.Right))
           {
               // If not down last update, key has just been pressed.
               if (!oldState.IsKeyDown(Keys.Right))
               {
                   spriteSpeed.X = 50;
                   spriteSpeed.Y = 0;
               }
           }
           else if (oldState.IsKeyDown(Keys.Right))
           {
               // Key was down last update, but not down now, so
               // it has just been released.
           }
           newState = oldState;
       }


Call that function from the UpdateSprite() loop

void UpdateSprite(GameTime gameTime)
       {
           // Move the sprite by speed, scaled by elapsed time.
           spritePosition +=
               spriteSpeed * (float)gameTime.ElapsedGameTime.TotalSeconds;
           int MaxX =
               graphics.GraphicsDevice.Viewport.Width - myTexture.Width;
           int MinX = 0;
           int MaxY =
               graphics.GraphicsDevice.Viewport.Height - myTexture.Height;
           int MinY = 0;
           // Check for bounce.
           if (spritePosition.X > MaxX)
           {
               spriteSpeed.X *= -1;
               spritePosition.X = MaxX;
           }
           else if (spritePosition.X < MinX)
           {
               spriteSpeed.X *= -1;
               spritePosition.X = MinX;
           }
           if (spritePosition.Y > MaxY)
           {
               spriteSpeed.Y *= -1;
               spritePosition.Y = MaxY;
           }
           else if (spritePosition.Y < MinY)
           {
               spriteSpeed.Y *= -1;
               spritePosition.Y = MinY;
           }
           // Check for user input directing Sprite
           UpdateInput();
       }

Sources

This solution is based on the tutorial by Microsoft that can be found at: MSDN Guide to Detecting Keyboard Input

Site Toolbox:

Personal tools
GNU Free Documentation License 1.2
This page was last modified on 17 March 2010, at 04:42.
Disclaimers - About BluWiki