2013-04-12 110 views
0

我和朋友決定爲自己製作遊戲。這是與一個寵物小精靈和舊的豐收月亮圖形樣式。我一直在做一些動畫測試,然後讓它工作。我的播放器Sprite Sheet有八個圖像(每個方向2個)。XNA/C#2D播放器動畫問題

但是,當我按住向上箭頭鍵和向左或向右或向下箭頭鍵和向左或向右時,它正嘗試同時執行兩個動畫。我知道必須有辦法解決這個問題,我只需要有人告訴我如何。

如果你感冒讓我知道如何解決我的問題,那就太棒了!

謝謝,獵人

這是我的動畫類,我爲我的球員:

public class Animation 
{ 
    Texture2D texture; 
    Rectangle rectangle; 
    public Vector2 position; 
    Vector2 origin; 
    Vector2 velocity; 

    int currentFrame; 
    int frameHeight; 
    int frameWidth; 

    float timer; 
    float interval = 75; 

    public Animation(Texture2D newTexture, Vector2 newPosition, int newFrameHeight, int newFrameWidth) 
    { 
     texture = newTexture; 
     position = newPosition; 
     frameWidth = newFrameWidth; 
     frameHeight = newFrameHeight; 
    } 

    public void Update(GameTime gameTime) 
    { 
     rectangle = new Rectangle(currentFrame * frameWidth, 0, frameWidth, frameHeight); 
     origin = new Vector2(rectangle.Width/2, rectangle.Height/2); 
     position = position + velocity; 

     // Comment Out For Camera!!!! 
     if (position.X <= 0 + 10) position.X = 0 + 10; 
     if (position.X >= 1920 - rectangle.Width + 5) position.X = 1920 - rectangle.Width + 5; 
     if (position.Y <= 0 + 10) position.Y = 0 + 10; 
     if (position.Y >= 1080 - rectangle.Height + 7) position.Y = 1080 - rectangle.Height + 7; 

     if (Keyboard.GetState().IsKeyDown(Keys.Right)) 
     { 
      AnimateRight(gameTime); 
      velocity.X = 2; 
     } 
     else if (Keyboard.GetState().IsKeyDown(Keys.Left)) 
     { 
      AnimateLeft(gameTime); 
      velocity.X = -2; 
     } 
     else velocity = Vector2.Zero; 

     if (Keyboard.GetState().IsKeyDown(Keys.Up)) 
     { 
      AnimateUp(gameTime); 
      velocity.Y = -2; 
     } 
     if (Keyboard.GetState().IsKeyDown(Keys.Down)) 
     { 
      AnimateDown(gameTime); 
      velocity.Y = 2; 
     } 
    } 

    public void AnimateRight(GameTime gameTime) 
    { 
      timer += (float)gameTime.ElapsedGameTime.TotalMilliseconds/2; 
      if (timer > interval) 
      { 
       currentFrame++; 
       timer = 0; 
       if (currentFrame > 1) 
        currentFrame = 0; 
      } 
    } 

    public void AnimateLeft(GameTime gameTime) 
    { 
      timer += (float)gameTime.ElapsedGameTime.TotalMilliseconds/2; 
      if (timer > interval) 
      { 
       currentFrame++; 
       timer = 0; 
       if (currentFrame > 3 || currentFrame < 2) 
        currentFrame = 2; 
      } 
    } 

    public void AnimateUp(GameTime gameTime) 
    { 
      timer += (float)gameTime.ElapsedGameTime.TotalMilliseconds/2; 
      if (timer > interval) 
      { 
       currentFrame++; 
       timer = 0; 
       if (currentFrame > 5 || currentFrame < 4) 
        currentFrame = 4; 
      } 
    } 

    public void AnimateDown(GameTime gameTime) 
    { 
      timer += (float)gameTime.ElapsedGameTime.TotalMilliseconds/2; 
      if (timer > interval) 
      { 
       currentFrame++; 
       timer = 0; 
       if (currentFrame > 7 || currentFrame < 6) 
        currentFrame = 6; 
      } 
    } 

    public void Draw(SpriteBatch spriteBatch) 
    { 
     spriteBatch.Draw(texture, position, rectangle, Color.White, 0f, origin, 1.0f, SpriteEffects.None, 0); 
    } 
} 

回答

0

這將允許在同一時間(我相信只有一個方向的運動是你想要的,如果什麼你是以下小寵物的遊戲風格):

if (Keyboard.GetState().IsKeyDown(Keys.Right)) 
    { 
     AnimateRight(gameTime); 
     velocity.X = 2; 
    } 
    else if (Keyboard.GetState().IsKeyDown(Keys.Left)) 
    { 
     AnimateLeft(gameTime); 
     velocity.X = -2; 
    }  
    else if (Keyboard.GetState().IsKeyDown(Keys.Up)) 
    { 
     AnimateUp(gameTime); 
     velocity.Y = -2; 
    } 
    else if (Keyboard.GetState().IsKeyDown(Keys.Down)) 
    { 
     AnimateDown(gameTime); 
     velocity.Y = 2; 
    } 
    else velocity = Vector2.Zero; 

如果你想兩個方向同時移動,這樣的:

bool animated = false; 
    if (Keyboard.GetState().IsKeyDown(Keys.Right)) 
    { 
     AnimateRight(gameTime); 
     velocity.X = 2; 
     animated = true; 
    } 
    else if (Keyboard.GetState().IsKeyDown(Keys.Left)) 
    { 
     AnimateLeft(gameTime); 
     velocity.X = -2; 
     animated = true; 
    }  
    else 
    { 
     velocity.X = 0; 
    } 

    if (Keyboard.GetState().IsKeyDown(Keys.Up)) 
    { 
     if(animated == false) 
     { AnimateUp(gameTime); } 

     velocity.Y = -2; 
    } 
    else if (Keyboard.GetState().IsKeyDown(Keys.Down)) 
    { 
     if(animated == false) 
     { AnimateDown(gameTime); } 
     velocity.Y = 2; 
    } 
    else velocity.Y = 0; 
0

我知道這已經有點老了,但是在這種情況下,如果兩個按鍵同時按下,那麼玩家無法控制哪一個會影響他將要移動的方向。一個更好的解決辦法是:

velocity = Vector2.Zero; 

if (Keyboard.GetState().IsKeyDown(Keys.Right)) 
    velocity.X += 2; 
if (Keyboard.GetState().IsKeyDown(Keys.Left)) 
    velocity.X -= 2; 

if (Keyboard.GetState().IsKeyDown(Keys.Up)) 
    velocity.Y -= 2; 
if (Keyboard.GetState().IsKeyDown(Keys.Down)) 
    velocity.Y += 2; 

if (velocity.X > 0) 
    AnimateRight(gameTime); 
else if (velocity.X < 0) 
    AnimateLeft(gameTime); 

// Animate Up/Down only if Left/Right does not... 
// not sure if needed but will follow the style. 
if (velocity.X == 0) 
{ 
    if (velocity.Y > 0) 
     AnimateDown(gameTime); 
    else if (velocity.Y < 0) 
     AnimateUp(gameTime); 
} 

這樣,我們分開輸入和邏輯,這使得事情更清晰,我們不會把優先於某些輸入(xept爲向上/向下...但w/e)如果一名球員同時嘗試左轉和右轉,該動作將被取消。