Archive

Archive for the ‘XNA’ Category

Fusion supports XNA!!!

May 21, 2008 4 comments

The day I’ve been waiting for has finally come – VMWare’s Fusion (Version 2.0b1 (89933)) supports XNA! I’ve only spent a few minutes playing around with it so far, but things are looking good. I’ve been able to create a blank project and the RPG starter kit project successfully.

Finally!! Ok, off to play with it some more.

Categories: Personal, Programming, XNA

Using the GamePadHelper class

I’ve had a little request to show how one might use the GamePadHelper class we wrote back in What a button was just pressed? so I will happily do so. Also, there was some confusion as to whether or not that the was the entire class code; it was! But I’ll post the complete class here. 

GamePadHelper.cs

/// <summary>
/// This class provides a set of helper utilities when working with the game pad.
/// </summary>
public static class GamePadHelper {

    private const int NumberOfPlayers = 4;

    private static GamePadState[] previousState = new GamePadState[NumberOfPlayers];
    private static GamePadState[] currentState = new GamePadState[NumberOfPlayers];

    /// <summary>
    /// The previous state of the game pad based on the last <see cref="Update"/> call.
    /// </summary>
    public static GamePadState[] PreviousState {
        get { return previousState; }
    }

    /// <summary>
    /// The current state of the game pad based on the current <see cref="Update"/> call.
    /// </summary>
    public static GamePadState[] CurrentState {
        get { return currentState; }
    }

    /// <summary>
    /// Updates the states used by the <see cref="GamePadHelper"/>.
    /// </summary>
    public static void Update() {
        for (int i = 0; i < NumberOfPlayers; i++) {
            previousState[i] = currentState[i];
            currentState[i] = GamePad.GetState((PlayerIndex)i);
        }
    }

    /// <summary>
    /// Determines if a button, or set of buttons, was down in <see cref="PreviousState"/> and
    /// is now up in <see cref="CurrentState"/>.
    /// </summary>
    /// <param name="playerIndex">The controller to check.</param>
    /// <param name="button">The button(s) to check.</param>
    /// <returns>True if the button state changed from pressed to released.</returns>
    public static bool WasButtonPressed(PlayerIndex playerIndex, Buttons button) {
        return previousState[(int)playerIndex].IsButtonDown(button) && currentState[(int)playerIndex].IsButtonUp(button);
    }
}

Using the code is quite simple. Let’s say we want to do something when Buttons.A is pressed. We’ll use the Update method that is in our game class to illustrate it’s use.

Code:

protected override void Update(GameTime gameTime) {
    GamePadHelper.Update();    // Remember to call this!!!

    if (GamePadHelper.WasButtonPressed(PlayerIndex.One, Buttons.A)) {
        // Our code to do something here.
    }
}

And really, it’s as simple as that.

Happy Coding!
-David

Categories: Programming, XNA Tags: ,