I have 2 sprites which have to be controlled each from a different player using a different game pad.
I created a class and put a property Vector2 direction. Inside that property I wrote the following.
GamePadState gamepadState1 = GamePad.GetState(PlayerIndex.One);
if (gamepadState1.ThumbSticks.Left.X != 0)
inputDirection.X += gamepadState1.ThumbSticks.Left.X;
and similar for Y
Now inside the Game1.cs I create 2 objects and I only need to write inside Update
player1.Update(gameTime, Game.Window.ClientBounds);
player2.Update(gameTime, Game.Window.ClientBounds);
In order to distinguish the one object from the other, I changed the sprite constructor and added a string. At the object creation the first has this named “left” and the second “right”
Moreover I changed the property direction inside the sprite class into the following:
GamePadState gamepadState1 = GamePad.GetState(PlayerIndex.One);
if (side == “left”)
{
if (gamepadState1.ThumbSticks.Left.X != 0)
inputDirection.X += gamepadState1.ThumbSticks.Left.X;
}
GamePadState gamepadState2 = GamePad.GetState(PlayerIndex.Two);
if (this.side == “right”)
{
if (gamepadState2.ThumbSticks.Left.X != 0)
inputDirection.X += gamepadState2.ThumbSticks.Left.X;
}
The problem is that left works fine with game pad One, but game pad Two does not bring any result to the game. Why?
left and right are correctly added to both objects, I check it with the debugger. And moving the second pad jumps correctly into the condition. For some reason the second object doesn’t realize that it should obey to the second game pad.
Any ideas?
Thanks