Wednesday, July 7

Added support for an Xbox 360 controller

Tab had loaned me his XNA book and his Xbox 360 controller here for me to play with a bit. So, what is it I'm doing first when I decide to code something more on the game? Of course, I add the support for the controller to the game :)

It was not hard at all, generally only these few lines:
GamePadState gamePad = GamePad.GetState(PlayerIndex.One);
bool moveLeft = Keyboard.GetState().IsKeyDown(Keys.Left)
    || gamePad.IsButtonDown(Buttons.DPadLeft);
bool moveRight = Keyboard.GetState().IsKeyDown(Keys.Right)
    || gamePad.IsButtonDown(Buttons.DPadRight);
bool run = Keyboard.GetState().IsKeyDown(Keys.LeftControl)
    || gamePad.IsButtonDown(Buttons.X);
bool jump = Keyboard.GetState().IsKeyDown(Keys.Up)
    || Keyboard.GetState().IsKeyDown(Keys.Space)
    || gamePad.IsButtonDown(Buttons.DPadUp)
    || gamePad.IsButtonDown(Buttons.A);

This and poof, the controller is now working with the game :) The rewriting of collision detection still remains -_-'

P.S. Yes, the controller works with Windows without any weird unofficial drivers or adapters. In fact, the controller is designed with a USB contact and has official Windows drivers for full support on Windows.

0 comments: