Time for action – handling input

  1. Add the following declarations to the declarations region of the Player class:
    private static Vector2 baseAngle = Vector2.Zero;
    private static Vector2 turretAngle = Vector2.Zero;
    private static float playerSpeed = 90f;
  2. Add the methods to handle keyboard and gamepad input to the Player class:
    #region Input Handling
    private static Vector2 handleKeyboardMovement(KeyboardState keyState)
        Vector2 keyMovement = Vector2.Zero;
        if (keyState.IsKeyDown(Keys.W))
        if (keyState.IsKeyDown(Keys.A))
        if (keyState.IsKeyDown(Keys.S))
        if (keyState.IsKeyDown(Keys.D))
        return keyMovement;
    private static Vector2 handleGamePadMovement(GamePadState gamepadState)
    { return ...

Get XNA 4.0 Game Development by Example Beginner's Guide now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.