Question

I want to use a XBOX 360 Controller to play sounds if the "A" Button is pressed. In my Code Snippet I've used a while loop to check if the Button is pressed. Are there any Event Handlers I can use or different solutions (It's not for Game Development)?

while (b == false)
{
    GamePadState currentState2 = GamePad.GetState(PlayerIndex.One);
    if (currentState2.IsConnected
         && currentState2.Buttons.A
                == Microsoft.Xna.Framework.Input.ButtonState.Pressed)
    {
         play1stTrack();
         isButtonApressed = true;

    }
}
Was it helpful?

Solution

Your update loop runs at 60 frames per second (Usually), you can use this time to check for input, and handle it. There is no need to write your own loop for input.

Example:

protected override void Update(GameTime gameTime)
{
    GamePadState currentState2 = GamePad.GetState(PlayerIndex.One);
    if (currentState2.IsConnected
         && currentState2.Buttons.A
                == Microsoft.Xna.Framework.Input.ButtonState.Pressed)
    {
         play1stTrack();
         isButtonApressed = true;
    }
    //Handle input with isButtonApressed
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top