Gamepad

From XNAWiki
Jump to: navigation, search

XNA supports the XBox360 gamepad. When using XNA with Visual Studio, Intellisense truly comes in handy.
If something is not in here, just start copying (typing) some of the code.
Try GamePad.GetState(PlayerIndex.One).Buttons.
Intellisense will show a list of rather usefull suggestions.

The following code can help you extract informatie from the gamepad. Check for connections:

GamePad.GetState(PlayerIndex.One).IsConnected //returns true if a gampad is connected as player one.
GamePad.GetState(PlayerIndex.Two).IsConnected //returns true if a second gamepad is connected. This can be done up to Four gamepads

Sticks:

Gamepad.GetState(PlayerIndex.One).Thumbsticks.Right.X //Returns the horizontal value of which way the stick is pushed.
//-1 is all the way to the left. 0 is idle and 1 is all the way to the right. 0.5 is halfway to the right.
 
GamePad.GetState(PlayerIndex.One).Thumbsticks.Right.Y //Returns the vertical value of the stick. Up is -1, idle is 0, down is 1.
 
GamePad.GetState(PlayerIndex.One).Thumbsticks.Left.X //Same as the first example but for the left stick.
GamePad.GetState(PlayerIndex.One).Thumbsticks.Left.Y //Same as the second example but for the left stick.

Buttons:

if (GamePad.GetState(PlayerIndex.One).Buttons.A == ButtonState.Pressed
{
//code here is executed while button A is down.
}
if (GamePad.GetState(PlayerIndex.One).Buttons.A == ButtonState.Released)
{
//code here is executed while button A  is released.
}

Triggers:

GamePad.GetState(PlayerIndex.One).Triggers.Left; //Returns a value between 0(idle) and 1(fully pressed) for the left trigger.
GamePad.GetState(PlayerIndex.One).Triggers.Right; //Returns a value between 0(idle) and 1(fully pressed) for the right trigger.

Reducing processing time:

Gamepad.Getstate means returning all of the buttons, sticks and triggers current value. You don't have to call it all the time because there's a less intensive way to do it. adding GamePadState Gamepad1 = GamePad.GetState(PlayerIndex.one) puts all of the Gamepad's variables in the variable Gamepad1. You can use Gamepad1 as if it were the actual gamepad.

GamePad.GetState(PlayerIndex.One).Buttons.RightShoulder;
GamePad.GetState(PlayerIndex.One).Buttons.LeftShoulder;
GamePad.GetState(PlayerIndex.One).Buttons.A;
GamePad.GetState(PlayerIndex.One).Buttons.B;

equals

GamePadState Gamepad1 = GamePad.GetState(PlayerIndex.one);
Gamepad1.Buttons.RightShoulder;
Gamepad1.Buttons.LeftShoulder;
Gamepad1.Buttons.A;
Gamepad1.Buttons.B;

Gamepadstates are mainly used for button tapping:

protected override void Update(GameTime gameTime)
{
GamePadState curGamepad1 = GamePad.GetState(PlayerIndex.one); //Retrieves gamepadstate for gamepad 1
...
if (curGamepad1.Buttons.A == ButtonState.Pressed && prevGamepad1.Buttons.A == ButtonState.Released) 
//The first condition is to make sure the button is pressed, the second condition is to make sure it wasn't on the previous frame.
{
...
}
...
prevStateGamepad1 = curGamepad1 //The current gamepadstate is being stored as the previous gamepadstate in preparation of the next frame. 
}

Vibration:

GamePad.SetVibration (PlayerIndex.One,0.5f,0.49f); // Makes player one's gamepad vibrate. It's left motor is at 50%, the right motor is at 49%
GamePad.SetVibration (PlayerIndex.One,0.5f,0.5f); // the default vibration
GamePad.SetVibration (PlayerIndex.One,0,0); // no vibration
GamePad.SetVibration (PlayerIndex.One,1,1); // max vibration

The following however, will not work.

GamePadState Gamepad1 = GamePad.GetState(PlayerIndex.one);
GamePad.SetVibration (Gamepad1,0.5f,0.5f);//A GamePadState (Gamepad1) is not a valid argument for SetVibration.