title: Making basic attack animation depending on keypress XNA C#
body: <p>I've got a 2D rpg-ish thing going on, where I want my character to swing a sword when I press a certain key, the sword swing is made out of 3 sprites.</p>
  
  <p>This is the code I got going on so far:
  EDIT: Upon request I've copypasted the entire class in where my attack-animation-code is in.</p>
  
  <pre><code>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 =&gt; t.Value.X == 78 &amp;&amp; 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) &amp;&amp; ks.IsKeyDown(Keys.Up))
          {
              direction = new Vector2(1, -1);
              directionPointer = 1;
              if (currentSprite.X != 140 &amp;&amp; currentSprite.X != 154)
                  currentSprite = new Rectangle(140, 30, 14, 16);
              if (timeSprite &gt;= 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) &amp;&amp; ks.IsKeyDown(Keys.Down))
          {
              direction = new Vector2(1, 1);
              directionPointer = 2;
              if (currentSprite.X != 229 &amp;&amp; currentSprite.X != 245)
                  currentSprite = new Rectangle(229, 30, 16, 16);
              if (timeSprite &gt;= 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) &amp;&amp; ks.IsKeyDown(Keys.Up))
          {
              direction = new Vector2(-1, -1);
              directionPointer = 1;
              if (currentSprite.X != 140 &amp;&amp; currentSprite.X != 154)
                  currentSprite = new Rectangle(140, 30, 14, 16);
              if (timeSprite &gt;= 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) &amp;&amp; ks.IsKeyDown(Keys.Down))
          {
              direction = new Vector2(-1, 1);
              directionPointer = 2;
              if (currentSprite.X != 229 &amp;&amp; currentSprite.X != 245)
                  currentSprite = new Rectangle(229, 30, 16, 16);
              if (timeSprite &gt;= 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 &amp;&amp; currentSprite.X != 214)
                  currentSprite = new Rectangle(198, 30, 16, 16);
              if (timeSprite &gt;= 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 &amp;&amp; currentSprite.X != 184)
                  currentSprite = new Rectangle(168, 30, 16, 16);
              if (timeSprite &gt;= 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 &amp;&amp; currentSprite.X != 154)
                  currentSprite = new Rectangle(140, 30, 14, 16);
              if (timeSprite &gt;= 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 &amp;&amp; currentSprite.X != 245)
                  currentSprite = new Rectangle(229, 30, 16, 16);
              if (timeSprite &gt;= 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) /*&amp;&amp; oldKs.IsKeyUp(Keys.A)*/)
          {
              isAnimationRunning = true;
          }
          while (isAnimationRunning &amp;&amp; directionPointer == 1)
          {
              attackTime += gameTime.ElapsedGameTime.Milliseconds;
              if (attackTime &gt; 1 &amp;&amp; attackTime &lt; 100)
              {
                  currentSprite = new Rectangle(3, 46, 32, 16);
              }
              if (attackTime &lt; 200 &amp;&amp; attackTime &gt; 100)
              {
                  currentSprite = new Rectangle(33, 61, 32, 32);
              }
              if (attackTime &gt; 200 &amp;&amp; attackTime &lt; 300)
              {
                  currentSprite = new Rectangle(61, 65, 16, 32);
              }
              if (attackTime &gt; 300)
              {
                  attackTime = 0;
                  currentSprite = new Rectangle(140, 30, 14, 16);
                  isAnimationRunning = false;
              }
          }
          if (directionPointer == 2 &amp;&amp; isAnimationRunning)
          {
              attackTime += gameTime.ElapsedGameTime.Milliseconds;
              if (attackTime &lt; 100)
              {
                  currentSprite = new Rectangle(4, 67, 32, 16);
              }
              else if (attackTime &lt; 200 &amp;&amp; attackTime &gt; 100)
              {
                  currentSprite = new Rectangle(35, 66, 32, 32);
              }
              else if (attackTime &gt; 200 &amp;&amp; attackTime &lt; 300)
              {
                  currentSprite = new Rectangle(69, 66, 16, 32);
              }
              else if (attackTime &gt; 300)
              {
                  attackTime = 0;
                  isAnimationRunning = false;
                  currentSprite = new Rectangle(229, 30, 16, 16);
              }
          }
          if (directionPointer == 3 &amp;&amp; isAnimationRunning)
          {
              attackTime += gameTime.ElapsedGameTime.Milliseconds;
              if (attackTime &lt; 100)
              {
                  currentSprite = new Rectangle(5, 138, 16, 32);
              }
              else if (attackTime &lt; 200 &amp;&amp; attackTime &gt; 100)
              {
                  currentSprite = new Rectangle(20, 138, 32, 32);
              }
              else if (attackTime &gt; 200 &amp;&amp; attackTime &lt; 300)
              {
                  currentSprite = new Rectangle(53, 154, 32, 16);
              }
              else if (attackTime &gt; 300)
              {
                  attackTime = 0;
                  isAnimationRunning = false;
                  currentSprite = new Rectangle(198, 30, 16, 16);
              }
          }
          if (directionPointer == 4 &amp;&amp; isAnimationRunning)
          {
              attackTime += gameTime.ElapsedGameTime.Milliseconds;
              if (attackTime &lt; 100)
              {
                  currentSprite = new Rectangle(5, 102, 16, 32);
              }
              else if (attackTime &lt; 200 &amp;&amp; attackTime &gt; 100)
              {
                  currentSprite = new Rectangle(20, 102, 32, 32);
              }
              else if (attackTime &gt; 200 &amp;&amp; attackTime &lt; 300)
              {
                  currentSprite = new Rectangle(51, 118, 32, 16);
              }
              else if (attackTime &gt; 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);
      }
  }
  }
  </code></pre>
  
  <p>Right now, it does not go past the first sprite of the attack when I swing, and I am not totally sure how to make it happen. I'd be grateful if someone could give me a tip or the like of how I could make it go through the entire code so that a full swing will be done on the press of a button? Thanks on beforehand!</p>