Game Development Stack Exchange is a question and answer site for professional and independent game developers. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I rework the code to make it more easier to manage. How can I force the player cannot change the sprite animation by pressing any keys until the attack animation completely ends?

class Player
    Animation playerIdle;
    Animation playerRun;
    Animation playerAtk;    

    public void Load(ContentManager content)
        playerIdle = new Animation(content, "idle", 144, 128, 8, 30);
        playerRun = new Animation(content, "run", 160, 112, 8, 30);
        playerAtk = new Animation(content, "atk", 154, 131, 2, 30);


    public void Update(GameTime gameTime)

    #region Update Control
    string selectSpriteSheet;

    KeyboardState mPreviousKeyboardState;
    SpriteEffects spriteEffects = SpriteEffects.FlipHorizontally;

    Vector2 feetPosition = new Vector2(0, 350);
    Vector2 mSpeed = Vector2.Zero;
    Vector2 mDirection = Vector2.Zero;
    Vector2 mStartingPosition = Vector2.Zero;

    int CHARACTER_SPEED = 50;
    int MOVE_LEFT = -5;
    int MOVE_RIGHT = 5;
    int MOVE_UP = -5;
    int MOVE_DOWN = 5;

    enum State

    State mCurrentState = State.Running;

    public void UpdateControl(GameTime gameTime)
        KeyboardState aCurrentKeyboardState = Keyboard.GetState();

        feetPosition += mDirection * mSpeed * (float)gameTime.ElapsedGameTime.TotalSeconds;
        selectSpriteSheet = "idle";
        UpdateMovement(aCurrentKeyboardState, gameTime);
        //UpdateJump(aCurrentKeyboardState, gameTime);

        mPreviousKeyboardState = aCurrentKeyboardState;

    private void UpdateMovement(KeyboardState aCurrentKeyboardState, GameTime gameTime)
        if (mCurrentState == State.Running)
            mSpeed = Vector2.Zero;
            mDirection = Vector2.Zero;

            if (aCurrentKeyboardState.IsKeyDown(Keys.Left) && !aCurrentKeyboardState.IsKeyDown(Keys.Right))

                selectSpriteSheet = "run";
                spriteEffects = SpriteEffects.None;
                mSpeed.X = CHARACTER_SPEED;
                mDirection.X = MOVE_LEFT;

            else if (aCurrentKeyboardState.IsKeyDown(Keys.Right) && !aCurrentKeyboardState.IsKeyDown(Keys.Left))

                selectSpriteSheet = "run";
                spriteEffects = SpriteEffects.FlipHorizontally;
                mSpeed.X = CHARACTER_SPEED;
                mDirection.X = MOVE_RIGHT;

            if (aCurrentKeyboardState.IsKeyDown(Keys.Z) && mPreviousKeyboardState.IsKeyUp(Keys.Z))
                mCurrentState = State.Attacking;

        if (mCurrentState == State.Attacking)

            selectSpriteSheet = "hit";
            mCurrentState = State.Running;

    public void Draw(SpriteBatch spriteBatch)
        //playerIdle.Draw(spriteBatch, feetPosition, (float)MathHelper.ToRadians(0), 1.0f);
        if (selectSpriteSheet == "idle")
            playerIdle.Draw(spriteBatch, feetPosition, spriteEffects, (float)MathHelper.ToRadians(0),1.0f);
        else if (selectSpriteSheet == "run")
            playerRun.Draw(spriteBatch, feetPosition, spriteEffects, (float)MathHelper.ToRadians(0),1.0f);
        else if (selectSpriteSheet == "hit")
            playerAtk.Draw(spriteBatch, feetPosition, spriteEffects, (float)MathHelper.ToRadians(0), 1.0f);
share|improve this question

The player class should have a currentAnimation and a nextAnimation.

If the currentAnimation is flagged as uninterruptible, then any attempts to change newAnimation are applied to nextAnimation and the currentAnimation is set to not repeat. Then, when the currentAnimation ends, nextAnimation will take its place.

share|improve this answer
currentAnimation is flagged as uninterruptible? How to do the flag? Sorry but I am a beginner. – optimisez Feb 27 '13 at 15:25
Make a boolean in your animation class. Make it true for the animations that are uninterruptible. – Byte56 Feb 27 '13 at 15:52

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.