Gamepad Input

If you're developing a game for Windows, you can still program for an Xbox 360 controller. You'll have to have a wired controller, or you can purchase an Xbox 360 Wireless Receiver for around $20, which will allow you to connect up to four wireless controllers to a PC.

Tip

The wireless Xbox 360 controller actually does come with a wire if you buy the charge pack for that controller, but there is no data transfer over that cable, so even when it's plugged in it's still a wireless controller. The cable on the charge pack transfers electricity for the charge, and nothing more.

Just as XNA provides a Mouse class for mouse input and a Keyboard class for keyboard input, it provides a GamePad class for reading input from an Xbox 360 gamepad. And yes, that's right, there's a GetState method for the GamePad class, just as there is for the other devices. There's something to be said for standards, and Microsoft's XNA Framework is, for the most part, a superb example of how standardization across a large-scale system (in this case, a framework and API) can be of such great benefit. Most of the time, you can tell how to use an object just by understanding the type of the object and knowing how similarly typed objects function. That's a tribute to a great design by the XNA team—kudos to them.

The GetState method for the GamePad class accepts an enum parameter called PlayerIndex that indicates which player's controller you want to access, and it returns a GamePadState struct that you ...

Get Learning XNA 3.0 now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.