2014-02-27 29 views
-1

當我放開左側或右側箭頭鍵時,我遇到了球員的滑動問題。我已經得到了這樣的結果,這樣玩家就會顯着減速,但是它的行爲就像是一堵牆,我想快速停下來,從玩家停止的速度延遲0.1到0.25。使用衝動的球員運動

代碼:

public void SlowDown() 
    { 
     float xForce; 
     float pushForce; 

     Vector2 left; 
     Vector2 right; 

     KeyboardState keyboardState = Keyboard.GetState(); 

     xForce = Person.Body.LinearVelocity.X; 

     if (Person.Body.LinearVelocity.X > 0 && !keyboardState.IsKeyDown(Keys.Right)) 
     { 
      if (Person.Body.LinearVelocity.X > 1) 
       pushForce = xForce + 1; 

      else 
       pushForce = 0; 

      right = new Vector2(-pushForce, 0); 

      Person.Body.ApplyForce(ref right); 
     } 
     else if (Person.Body.LinearVelocity.X < 0 && !keyboardState.IsKeyDown(Keys.Left)) 
     { 
      if (Person.Body.LinearVelocity.X < -1) 
       pushForce = xForce - 1; 

      else 
       pushForce = 0; 

      left = new Vector2(-pushForce, 0); 

      Person.Body.ApplyForce(ref left); 
     } 
     else 
     { 
     } 
    } 

回答

0

也許你需要所謂的 「摩擦」。這就是我實現了一個字符的摩擦我的遊戲:

public class Character 
{ 
    private FrictionStruct friction; 
    private Stopwatch frictionTimer; 

public enum Direction : byte 
{ 
    Up = 0, 
    Up_Left = 1, 
    Up_Right = 2, 
    Down = 3, 
    Down_Left = 4, 
    Down_Right = 5, 
    Left = 6, 
    Right = 7, 
    None = 8 
} 

    public struct FrictionStruct 
{ 
    public float moveLeftValue; 
    public float moveRightValue; 
    public float jumpValue; 
    public float fallValue; 

    public float moveLeft; 
    public float moveRight; 
    public float jump; 
    public float fall; 
    public float move; 
} 

    public Dictionary<Direction, bool> AllowMovement { get; private set; } 
    public Vector2 CalculatedMovement { get; private set; } 
    public Vector2 Position { get; private set; } 
    public Vector2 LastPosition { get; private set; } 
    public Direction LastDirection { get; private set; } 

public Character() 
{ 
    frictionTimer = Stopwatch.StartNew(); 
     AllowMovement = new Dictionary<Direction, bool>(); 
     for (byte i = 0; i < Enum.GetNames(typeof(Direction)).Length; i++) 
      AllowMovement.Add((Direction)i, true); 
} 
     private void CalculateFriction(TimeSpan time, float move_increment, float move_decrement, float fall_increment, float jump_decrement, float moveMaxFriction, float fallMaxFriction, float jumpMaxFriction, bool isMove) 
    { 
     if (frictionTimer.Elapsed >= time) 
     { 
      switch (LastDirection) 
      { 
       case Direction.Left: 
        if (isMove) 
        { 
         if (friction.moveLeftValue < moveMaxFriction) 
          friction.moveLeftValue += move_increment; 
        } 
        else 
        { 
         if (friction.moveLeftValue > 0) 
          friction.moveLeftValue -= move_decrement; 
        } 
        if (friction.moveRightValue > 0) 
         friction.moveRightValue -= move_decrement; 
        break; 
       case Direction.Right: 
        if (isMove) 
        { 
         if (friction.moveRightValue < moveMaxFriction) 
          friction.moveRightValue += move_increment; 
        } 
        else 
        { 
         if (friction.moveRightValue > 0) 
          friction.moveRightValue -= move_decrement; 
        } 
        if (friction.moveLeftValue > 0) 
         friction.moveLeftValue -= move_decrement; 
        break; 
      } 
      if (Jump) 
      { 
       if (friction.jumpValue > 0) 
        friction.jumpValue -= jump_decrement; 
       if (friction.fallValue != 0) 
        friction.fallValue = 0; 
      } 
      else 
      { 
       if (friction.fallValue < fallMaxFriction) 
        friction.fallValue += fall_increment; 
       if (friction.jumpValue != jumpMaxFriction) 
        friction.jumpValue = jumpMaxFriction; 
      } 
      if (friction.jumpValue < 0) 
       friction.jumpValue = 0; 
      if (friction.moveLeftValue < 0) 
       friction.moveLeftValue = 0; 
      if (friction.moveRightValue < 0) 
       friction.moveRightValue = 0; 
      frictionTimer.Restart(); 
     } 
     friction.jump = friction.jumpValue; 
     friction.fall = friction.fallValue; 
     friction.moveLeft = friction.moveLeftValue; 
     friction.moveRight = friction.moveRightValue; 
     friction.move = friction.moveLeftValue + friction.moveRightValue; 
    } 
public void UpdateChararacterMovement(Direction direction, GameTime gameTime) 
{ 
    LastPosition = Position; 
    LastDirection = direction; 
    CalculateFriction(TimeSpan.FromMilliseconds(100), 0.2f, 0.2f, 0.2f, 0.2f, 1, 1, 1, IsMove); 
     if (AllowMovement[direction]) 
     { 
      if (friction.moveLeft > 0 && AllowMovement[Direction.Left]) 
      { 
       if (intersectsWithObject[0] && LastDirection == Direction.Right) 
       { 
       } 
       else 
       { 
        CalculatedMovement = DirectionalVector.Vector(Direction.Left) * MoveSpeed * friction.moveLeft * (float)gameTime.ElapsedGameTime.TotalSeconds; 
        Position += CalculatedMovement; 
       } 
      } 
      if (friction.moveRight > 0 && AllowMovement[Direction.Right]) 
      { 
       if (intersectsWithObject[1] && LastDirection == Direction.Left) 
       { 
       } 
       else 
       { 
        CalculatedMovement = DirectionalVector.Vector(Direction.Right) * MoveSpeed * friction.moveRight * (float)gameTime.ElapsedGameTime.TotalSeconds; 
        Position += CalculatedMovement; 
       } 
      } 
     } 
     else 
     { 
      friction.moveLeftValue = 0; 
      friction.moveRightValue = 0; 
     } 
} 
} 

您需要使用CalculateFriction方法在你的更新代碼,那麼就乘以你的運動矢量右/左friction.right/friction.left;像這樣:

right = new Vector2(-pushForce, 0) * friction.right; 

請注意,如果您在遊戲中使用物理,摩擦無法正常工作。 我推薦使用ElapsedGameTime以避免像你的性格的運動FPS下降的問題:

right = new Vector2(-pushForce, 0) * friction.right * (float)gameTime.ElapsedGameTime.TotalSeconds; 
+0

謝謝你的信息。我認爲這會起作用。目前我一直在做其他事情,無法訪問我的代碼進行測試和編輯。 –