2014-01-10 45 views
0

我有一個2D角色扮演遊戲正在進行,當我按下某個鍵時,我希望我的角色擺動一把劍,劍的擺動是由3個角色組成的。製作基本攻擊動畫取決於按鍵XNA C#

這是我竟能迄今代碼: 編輯:根據要求,我copypasted在我的攻擊動畫代碼是在整個類

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Text; 
using Microsoft.Xna.Framework; 
using Microsoft.Xna.Framework.Audio; 
using Microsoft.Xna.Framework.Content; 
using Microsoft.Xna.Framework.GamerServices; 
using Microsoft.Xna.Framework.Graphics; 
using Microsoft.Xna.Framework.Input; 
using Microsoft.Xna.Framework.Media; 

namespace RPGJensLomanderV2 
{ 
class Player : Characters 
{ 
    KeyboardState ks; 
    Camera camera; 
    KeyboardState oldKs; 
    int attackTime; 
    Vector2 origin = Vector2.Zero; 
    bool isAnimationRunning = false; 

    public Player(GameScreen gameScreen, Camera camera) 
     : base(gameScreen) 
    { 
     health = 10; 
     this.camera = camera; 
     direction = Vector2.Zero; 
     movementSpeed = 1; 
     currentSprite = new Rectangle(229, 30, 14, 16); 
    } 

    public override void Load(ContentManager content) 
    { 
     spawnPoint = gameScreen.destSourceRecISE.Where(t => t.Value.X == 78 && t.Value.Y == 26).First().Key; 
     position = spawnPoint; 
     gameScreen.destSourceRecISE.Remove(spawnPoint); 
     base.Load(content); 
    } 

    public override void Update(GameTime gameTime) 
    { 
     ks = Keyboard.GetState(); 
     timeSprite += gameTime.ElapsedGameTime.Milliseconds; 
     Attack(gameTime); 
     attackTime = 0; 
     Movement(gameTime); 
     camera.Position = position; 
     oldKs = ks; 
    } 

    private void Movement(GameTime gameTime) 
    { 

     if (ks.IsKeyDown(Keys.Right) && ks.IsKeyDown(Keys.Up)) 
     { 
      direction = new Vector2(1, -1); 
      directionPointer = 1; 
      if (currentSprite.X != 140 && currentSprite.X != 154) 
       currentSprite = new Rectangle(140, 30, 14, 16); 
      if (timeSprite >= 100) 
      { 
       if (currentSprite.X == 140) 
        currentSprite = new Rectangle(154, 30, 14, 16); 
       else if (currentSprite.X == 154) 
        currentSprite = new Rectangle(140, 30, 14, 16); 
       timeSprite = 0; 
      } 
     } 
     else if (ks.IsKeyDown(Keys.Right) && ks.IsKeyDown(Keys.Down)) 
     { 
      direction = new Vector2(1, 1); 
      directionPointer = 2; 
      if (currentSprite.X != 229 && currentSprite.X != 245) 
       currentSprite = new Rectangle(229, 30, 16, 16); 
      if (timeSprite >= 100) 
      { 
       if (currentSprite.X == 229) 
        currentSprite = new Rectangle(245, 30, 16, 16); 
       else if (currentSprite.X == 245) 
        currentSprite = new Rectangle(229, 30, 16, 16); 
       timeSprite = 0; 
      } 
     } 
     else if (ks.IsKeyDown(Keys.Left) && ks.IsKeyDown(Keys.Up)) 
     { 
      direction = new Vector2(-1, -1); 
      directionPointer = 1; 
      if (currentSprite.X != 140 && currentSprite.X != 154) 
       currentSprite = new Rectangle(140, 30, 14, 16); 
      if (timeSprite >= 100) 
      { 
       if (currentSprite.X == 140) 
        currentSprite = new Rectangle(154, 30, 14, 16); 
       else if (currentSprite.X == 154) 
        currentSprite = new Rectangle(140, 30, 14, 16); 
       timeSprite = 0; 
      } 
     } 
     else if (ks.IsKeyDown(Keys.Left) && ks.IsKeyDown(Keys.Down)) 
     { 
      direction = new Vector2(-1, 1); 
      directionPointer = 2; 
      if (currentSprite.X != 229 && currentSprite.X != 245) 
       currentSprite = new Rectangle(229, 30, 16, 16); 
      if (timeSprite >= 100) 
      { 
       if (currentSprite.X == 229) 
        currentSprite = new Rectangle(245, 30, 16, 16); 
       else if (currentSprite.X == 245) 
        currentSprite = new Rectangle(229, 30, 16, 16); 
       timeSprite = 0; 
      } 
     } 
     else if (ks.IsKeyDown(Keys.Left)) 
     { 
      direction = new Vector2(-1, 0); 
      directionPointer = 3; 
      if (currentSprite.X != 198 && currentSprite.X != 214) 
       currentSprite = new Rectangle(198, 30, 16, 16); 
      if (timeSprite >= 100) 
      { 
       if (currentSprite.X == 198) 
        currentSprite = new Rectangle(214, 30, 15, 16); 
       else if (currentSprite.X == 214) 
        currentSprite = new Rectangle(198, 30, 16, 16); 
       timeSprite = 0; 
      } 
     } 
     else if (ks.IsKeyDown(Keys.Right)) 
     { 
      direction = new Vector2(1, 0); 
      directionPointer = 4; 
      if (currentSprite.X != 168 && currentSprite.X != 184) 
       currentSprite = new Rectangle(168, 30, 16, 16); 
      if (timeSprite >= 100) 
      { 
       if (currentSprite.X == 168) 
        currentSprite = new Rectangle(184, 30, 16, 16); 
       else if (currentSprite.X == 184) 
        currentSprite = new Rectangle(168, 30, 16, 16); 
       timeSprite = 0; 
      } 
     } 
     else if (ks.IsKeyDown(Keys.Up)) 
     { 
      direction = new Vector2(0, -1); 
      directionPointer = 1; 
      if (currentSprite.X != 140 && currentSprite.X != 154) 
       currentSprite = new Rectangle(140, 30, 14, 16); 
      if (timeSprite >= 100) 
      { 
       if (currentSprite.X == 140) 
        currentSprite = new Rectangle(154, 30, 14, 16); 
       else if (currentSprite.X == 154) 
        currentSprite = new Rectangle(140, 30, 14, 16); 
       timeSprite = 0; 
      } 
     } 
     else if (ks.IsKeyDown(Keys.Down)) 
     { 
      direction = new Vector2(0, 1); 
      directionPointer = 2; 
      if (currentSprite.X != 229 && currentSprite.X != 245) 
       currentSprite = new Rectangle(229, 30, 16, 16); 
      if (timeSprite >= 100) 
      { 
       if (currentSprite.X == 229) 
        currentSprite = new Rectangle(245, 30, 16, 16); 
       else if (currentSprite.X == 245) 
        currentSprite = new Rectangle(229, 30, 16, 16); 
       timeSprite = 0; 
      } 
     } 
     else 
     { 
      direction = Vector2.Zero; 
      timeSprite = 0; 
     } 
     position += movementSpeed * direction; 
    } 

    private void Attack(GameTime gameTime) 
    { 
     if (ks.IsKeyDown(Keys.A) /*&& oldKs.IsKeyUp(Keys.A)*/) 
     { 
      isAnimationRunning = true; 
     } 
     while (isAnimationRunning && directionPointer == 1) 
     { 
      attackTime += gameTime.ElapsedGameTime.Milliseconds; 
      if (attackTime > 1 && attackTime < 100) 
      { 
       currentSprite = new Rectangle(3, 46, 32, 16); 
      } 
      if (attackTime < 200 && attackTime > 100) 
      { 
       currentSprite = new Rectangle(33, 61, 32, 32); 
      } 
      if (attackTime > 200 && attackTime < 300) 
      { 
       currentSprite = new Rectangle(61, 65, 16, 32); 
      } 
      if (attackTime > 300) 
      { 
       attackTime = 0; 
       currentSprite = new Rectangle(140, 30, 14, 16); 
       isAnimationRunning = false; 
      } 
     } 
     if (directionPointer == 2 && isAnimationRunning) 
     { 
      attackTime += gameTime.ElapsedGameTime.Milliseconds; 
      if (attackTime < 100) 
      { 
       currentSprite = new Rectangle(4, 67, 32, 16); 
      } 
      else if (attackTime < 200 && attackTime > 100) 
      { 
       currentSprite = new Rectangle(35, 66, 32, 32); 
      } 
      else if (attackTime > 200 && attackTime < 300) 
      { 
       currentSprite = new Rectangle(69, 66, 16, 32); 
      } 
      else if (attackTime > 300) 
      { 
       attackTime = 0; 
       isAnimationRunning = false; 
       currentSprite = new Rectangle(229, 30, 16, 16); 
      } 
     } 
     if (directionPointer == 3 && isAnimationRunning) 
     { 
      attackTime += gameTime.ElapsedGameTime.Milliseconds; 
      if (attackTime < 100) 
      { 
       currentSprite = new Rectangle(5, 138, 16, 32); 
      } 
      else if (attackTime < 200 && attackTime > 100) 
      { 
       currentSprite = new Rectangle(20, 138, 32, 32); 
      } 
      else if (attackTime > 200 && attackTime < 300) 
      { 
       currentSprite = new Rectangle(53, 154, 32, 16); 
      } 
      else if (attackTime > 300) 
      { 
       attackTime = 0; 
       isAnimationRunning = false; 
       currentSprite = new Rectangle(198, 30, 16, 16); 
      } 
     } 
     if (directionPointer == 4 && isAnimationRunning) 
     { 
      attackTime += gameTime.ElapsedGameTime.Milliseconds; 
      if (attackTime < 100) 
      { 
       currentSprite = new Rectangle(5, 102, 16, 32); 
      } 
      else if (attackTime < 200 && attackTime > 100) 
      { 
       currentSprite = new Rectangle(20, 102, 32, 32); 
      } 
      else if (attackTime > 200 && attackTime < 300) 
      { 
       currentSprite = new Rectangle(51, 118, 32, 16); 
      } 
      else if (attackTime > 300) 
      { 
       attackTime = 0; 
       currentSprite = new Rectangle(168, 30, 16, 16); 
       isAnimationRunning = false; 
      } 
     } 
    } 

    public override void Draw(GameTime gameTime, SpriteBatch spriteBatch) 
    { 
     spriteBatch.Draw(texture, position, currentSprite, Color.White, 0f, origin, 1f, SpriteEffects.None, 0f); 
    } 
} 
} 

眼下,它的作用。當我揮杆的時候,不要超越攻擊的第一個精靈,我不完全確定如何使它發生。如果有人能給我一個提示或者類似的提示,我可以讓它通過整個代碼,以便按下一個按鈕就可以完成一個完整的操作。事先感謝!

+0

如果你保持按下按鈕,然後它的工作原理? –

回答

1

定義一個全局變量

bool isAnimationRunning = false; 

,然後改變你這樣的代碼:

if (ks.IsKeyDown(Keys.A) && directionPointer == 1 && oldKs.IsKeyUp(Keys.A)) 
{ 
    isAnimationRunning = true; 
} 

if(isAnimationRunning) 
{ 
    attackTime += gameTime.ElapsedGameTime.Milliseconds; 
    if (attackTime > 1 && attackTime < 100) 
    { 
     currentSprite = new Rectangle(3, 46, 32, 16); 
    } 
    else if (attackTime < 200 && attackTime > 100) 
    { 
     currentSprite = new Rectangle(33, 61, 32, 32); 
    } 
    else if (attackTime > 200 && attackTime < 300) 
    { 
     currentSprite = new Rectangle(61, 65, 16, 32); 
    } 
    else if (attackTime > 300) 
    { 
     attackTime = 0; 
     isAnimationRunning = false; 
    } 
} 
+0

我想說這是有效的,但它不夠奇怪。這應該。它看起來很正確o.o –

+0

IsAnimationRunning是全局定義的,還是在方法內定義的? –

+0

它是全局定義的,當按鈕被按下時,他開始攻擊,所以這部分工作。現在我的角色不再是一旦我停止移動就揮動他的劍,而是凍結在第一個攻擊精靈身上。所以基本上,我認爲代碼不會達到最後一個代碼塊。嘗試刪除其他的,並試圖讓一段時間(isAnimationRunning),而沒有幫助。 –