0

在 c# 中编码并使用 XNA 4.0 框架我正在尝试为玩家控制开发键盘和游戏控制器输入。

我的游戏控制器输入代码如下;

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();
     }
}

这会按预期移动玩家,并且精灵会翻转到正确的方向,但动画片段不起作用。精灵应该在输入时随着玩家的移动而动画。这在从键盘输入时效果很好,见下文;

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();
       }
}

我很难弄清楚为什么它适用于键盘输入而不适用于游戏控制器输入。它几乎看起来好像它正在尝试制作动画,但从未完全超过第 3 个动画单元格,或者动画速度如此之快以至于看起来好像几乎没有动画。任何帮助将不胜感激!

4

1 回答 1

0

我最终添加了

if (gamepadState.IsConnected){//Gamepad code}

if (!gamepadState.IsConnected){//Keyboard code}

单独阅读输入似乎已经解决了这个问题。

不过感谢大家的帮助。

于 2013-01-30T02:11:08.247 回答