Using The Xbox Controller Class

The xbox controller class is an event driven controller model. To use it you must first create a new instance of the class.

XboxController myController = new XboxController(PlayerIndex.One);

This line of code will create a new instance of the controller class that will be linked to the first players controller.
This will still do us no good because this object still works in the normal loop of XNA so we need to update it. This is easy enough to do simply add

myController.Update(gameTime); to your game's update function and you will be ready to go.

ok so we have a controller set up and we have it updating now we just need to tell it to do stuff when buttons are pressed this is easy to do.

put this line of code in the initilize function of the class containing your controller object AFTER you have instantiated the controller object or else you will get a null object reference exception.

myController.aButtonPressed += myFunction;

this will link the aButtonPressed Event to myFunction and when you press the A button on the xbox controller. There are also events for when a button is released, and properties to check if a button is down. If needed you can also manually check the states of buttons, using the myController.CurrentGamePadState property.

Last edited Jul 23, 2010 at 9:08 PM by Aeal, version 1


No comments yet.