This last week I have been porting our game, Jojamian, to the Xbox 360. Two of the largest things that I must convert is input and graphics.
Today I am going to explain a little about input, and give you some sample code that allows GamePad users on the 360 to navigate using the same cursor as their PC friends.
You can look up the MouseState code on the MSDN Library for XNA Game Studio Express 3.0. For the sake of KISS, I’m just going to show you the translation of that for the GamePad.
In the class declaration for your Game.cs, add the following:
//Variables for X and Y position of the cursor
public static int xbCursorX;
public static int xbCursorY;
//Initializing GamePad State
public static GamePadState gamePadState =
GamePad.GetState(PlayerIndex.One);
In the main Game.cs, this was added to Draw();
//Update the state of gamePadState
gamePadState = GamePad.GetState(PlayerIndex.One);
//Update cursor for 360 controller input
if (gamePadState.ThumbSticks.Left.X > 0.0f)
xbCursorX += 5;if (gamePadState.ThumbSticks.Left.X < 0.0f)
xbCursorX -= 5;if (gamePadState.ThumbSticks.Left.Y > 0.0f)
xbCursorY -= 5;if (gamePadState.ThumbSticks.Left.Y < 0.0f)
xbCursorY += 5;
Our Cursor.cs handles where the cursor is drawn:
PC: this.position = new Vector2(mouseState.X, mouseState.Y);
360:this.position = new Vector2(Game.xbCursorX, Game.xbCursorY);
There are two things to note with this cursor input. First of all, the bounds for the cursor are not set, so the user can take the cursor off the screen. Second, I am getting an error with the code. It seems that rapidly tapping left twice on the analog stick causes it to keep moving the cursor to the left. I have yet to deduce whether it is a mechanical error with my single controller or a problem in the code.
Try implementing the same coding concepts into your game and see how it turns out. If it works for you, tell me about it in the comments. If you have a better way of doing it, by all means feel free to comment below as well. GL and GG.
No comments:
Post a Comment