GamePad State and Button Pressed
Saturday, March 03 2007 - gse, xna, tutorial
With one of the systems that I am currently working on I need to be able to tell wether the button has been pressed then released, to do this I have employed the process of having a previous game pad state stored inside my application. Now I know that this is common practice but in doing so I thought I would expand on the default game template that ships with the Game Studio Express and add the functions.
To start open up the Game Studio Express IDE and create a default Windows Game (Or a 360 game as the process will work with either). Next add the following line just underneath the declaration of the content manager.
GamePadState previousGamePadState;
It should now look like this.
GraphicsDeviceManager graphics;
ContentManager content;
GamePadState previousGamePadState;
The next step is to moderfy the Update Call in the Application, browse down to the Update call and change the following lines...
// Allows the default game to exit on Xbox 360 and Windows
if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed)
this.Exit();
So that they look like the following...
GamePadState currentState = GamePad.GetState(PlayerIndex.One);
if (currentState.IsConnected)
{
// Allows the default game to exit on Xbox 360 and Windows
if (currentState.Buttons.Back == ButtonState.Pressed &&
previousGamePadState.Buttons.Back == ButtonState.Released)
{
this.Exit();
}
previousGamePadState = currentState;
}
With the above changes you should now be able to allow the user to hold down a button and the action will only happen once the user releases the button.
I hope that this has help some one, one thing that you might also be able to do now is export this out as a template and use it for future applications as your base. If anyone is interested I could write about the process to do this...
Similar Posts
- Tutorial 01 - XNA Your First Windows Game
- Getting Started with Blender 3D and XNA
- Getting Started with Blender 3D and XNA
