Skip to Main Content
Learning XNA 3.0
book

Learning XNA 3.0

by Aaron Reed
November 2008
Beginner content levelBeginner
510 pages
16h 24m
English
O'Reilly Media, Inc.
Content preview from Learning XNA 3.0

Converting the Collision Game's Player Input Code

In the Update method of the Game1 class, you use the Keyboard object to see whether the user has pressed any keys to start the game when it is in the GameState.Start game state. Your code looks something like this:

if (Keyboard.GetState().GetPressedKeys(  ).Length > 0)
{
    currentGameState = GameState.InGame;
    spriteManager.Enabled = true;
    spriteManager.Visible = true;
}

Change that code to the following to check for any key being pressed in the Windows project and to check for the Play/Pause button being pressed in the Zune project:

#if !ZUNE
    if (Keyboard.GetState().GetPressedKeys(  ).Length > 0)
#else
    if(GamePad.GetState(PlayerIndex.One).Buttons.B == ButtonState.Pressed)
#endif
    {
        currentGameState = GameState.InGame;
        spriteManager.Enabled = true;
        spriteManager.Visible = true;
    }

You do something similar later in the Update method, in the GameState.GameOver game state. That is, you check to see whether the player presses the Enter key and, if so, exit the game:

if (Keyboard.GetState(  ).IsKeyDown(Keys.Enter))
    Exit(  );

Change that code to the following:

#if !ZUNE
    if (Keyboard.GetState(  ).IsKeyDown(Keys.Enter))
#else
    if(GamePad.GetState(PlayerIndex.One).Buttons.B == ButtonState.Pressed)
#endif
    Exit(  );

The next change you'll need to make is in the UserControlledSprite class. In the direction accessor, you have code that uses the keyboard to move the player object around the screen:

if (Keyboard.GetState( ).IsKeyDown(Keys.Left)) inputDirection.X −= ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Start your free trial

You might also like

Learning XNA 4.0

Learning XNA 4.0

Aaron Reed
Beginning C# 7 Programming with Visual Studio 2017

Beginning C# 7 Programming with Visual Studio 2017

Benjamin Perkins, Jacob Vibe Hammer, Jon D. Reid

Publisher Resources

ISBN: 9780596154905Supplemental ContentErrata Page