Coding in c# and using the XNA 4.0 framework I am trying to develop for both keyboard and game controller input when it comes to player control.
My code for the game controller input is as follows;
GamePadState gamepadState = GamePad.GetState(PlayerIndex.One);
if(gamepadState.ThumbSticks.Left.X != 0 || gamepadState.ThumbSticks.Left.Y != 0)
{
//Handles rotation
angle += thumbsticksMove(gamepadState); //handles Left.X and Left.Y input
normalize(); //normalizes angle and sets normalizedAngle = angle
this.Rotate(normalizedAngle); //takes value and passes it through Math helper
//atan and pi*2
//Ends handles rotation
pos += (angle * speed);
//Implementing framerate adjustment just for this class
timeSinceLastFrame += (float)gameTime.ElapsedGameTime.Milliseconds;
if (timeSinceLastFrame > millisecondsPerFrame)
{
timeSinceLastFrame -= millisecondsPerFrame;
Animation();
}
}
This moves the player as expected and the sprite flips to the proper direction, but the animation piece does not work. The sprite is supposed to animate with player movement upon input. This works great when taking in input from the keyboard, see below;
if (Keyboard.GetState().IsKeyDown(Keys.Right))
{
input = Vector2.Zero;
input.X = 1;
//Handles rotation
angle.X = input.X;
normalize();
this.Rotate(normalizedAngle);
//Ends handles rotation
pos += (input * speed);
//Implementing framerate adjustment just for this class
timeSinceLastFrame += (float)gameTime.ElapsedGameTime.Milliseconds;
if (timeSinceLastFrame > millisecondsPerFrame)
{
timeSinceLastFrame -= millisecondsPerFrame;
Animation();
}
}
I’m having a hard time figuring out why it works for the keyboard input but not the game controller input. It almost looks as if it’s trying to animate but never quite makes it past the 3rd animation cell or animates so fast to the point of looking as if it’s barely animating. Any help would be hugely appreciated!
I ended up adding
Reading the inputs separately seems to have fixed the issue.
Thanks for everyone’s help though.