Answer: The key to tracking input from the Xbox 360 controller is the GamePadState class. To track if a controller button is down, we want to check the ButtonState of the button in a GamePadState.
First, inside of the Update method, create a GamePadState variable:
GamePadState gamePad = GamePad.GetState(PlayerIndex.One);
After creating the GamePadState, we then initialize it with the current value of player one's game pad. Since this code is processed every Update method, the value that our GamePadState contains is always valid.
Now, we can check to the value of the controller. In the Update method, you would add something like this to check if a button is pressed:
if (gamePad.Buttons.A == ButtonState.Pressed)
{
//Code
}
In the above if-statement, we ask if the A button is pressed. If it is, then the code inside the if-statement will be processed. You can also check if the button is released by checking against ButtonState.Released.
No comments:
Post a Comment