views:

111

answers:

1

I've been working on this for quite some time looking for solutions in various places. I've used Nick Gravelyn's style of storing animations into a text file and am simply incrementing an index to change frames. The trouble I'm having is looping an animation once and only once, but for some reason the way that I know should work isn't working the way I thought it would. I can't for the life of me figure out why, unless it's very specific to how XNA works.

Here is my code:

private void UpdateAttack(KeyboardState current, KeyboardState last, GameTime gameTime)
        {
           if (current.IsKeyDown(Keys.S) && last.IsKeyUp(Keys.S))
           {
               neutralStandingKick(gameTime);
           }
        }   

        private void neutralStandingKick(GameTime gameTime)
        {
            //timeSinceLastFrame += gameTime.ElapsedGameTime.Milliseconds; //Framerate control
            //if (timeSinceLastFrame > millisecondsPerFrame) //Framerate control
            //{
                //timeSinceLastFrame -= millisecondsPerFrame; //Framerate control
                if (mCurrentState != State.Kicking)
                {
                    mCurrentState = State.Kicking;

                    Position.Y = 200;

                    loopOnce(25, 30); //Starts at frame 25, ends at 30
                }
            //}
        }

        private void loopOnce(int min, int max)
        {
            if (currentImageIndex > max || currentImageIndex < min) //Checks to see if index out of range of current animation
                currentImageIndex = min; //Starts at the beginning of the animation
            for (int i = min; i < max; i++) //Uses the range to determine when to stop
            { currentImageIndex++; } //Increments index each iteration that passes
        }

Edit: Here is the Draw method of this particular class

public void Draw(SpriteBatch spriteBatch)
{
    //Get the name of the current sprite to draw
    string spriteName = possibleImages[currentImageIndex];

    //Use that to get the source rectangle
    Rectangle source = spriteSourceRectangles[spriteName];

    //Send rectangle to a function to set the framesize for bounds checking
    getFrameSize(source);

    spriteBatch.Draw(theSpriteSheet, Position, source, Color.White);
}

private void getFrameSize(Rectangle frame)
{
    frameSize = frame; //For bounds checking
}

Why wouldn't this work?

New Code (Gavin's Suggestion):

private void UpdateAttack(KeyboardState current, KeyboardState last, GameTime gameTime)
{
    const int min = 22;
    const int max = 30;

    timeSinceLastFrame += gameTime.ElapsedGameTime.Milliseconds; //Framerate control

           if (current.IsKeyDown(Keys.S) && mCurrentState != State.Kicking)
           {
               mCurrentState = State.Kicking;
               currentImageIndex = min;
           }
           if (mCurrentState == State.Kicking)
           {
               if (timeSinceLastFrame > millisecondsPerFrame) //Framerate control
               {
                   timeSinceLastFrame -= millisecondsPerFrame; //Framerate control
                   currentImageIndex++;
               }
           }
           if (currentImageIndex == max)
               mCurrentState = State.Idle;
}

Method that calls UpdateAttack:

public void Update(GameTime theGameTime, Game game)
{
    KeyboardState aCurrentKeyboardState = Keyboard.GetState();

    UpdateMovement(aCurrentKeyboardState);
    UpdateJump(aCurrentKeyboardState);
    UpdateAttack(aCurrentKeyboardState, mPreviousKeyboardState, theGameTime);
    UpdateStageBounds(game);

    mPreviousKeyboardState = aCurrentKeyboardState;
}

It will loop the animation on holding the keyboard key "s" down. But it will not loop all 7 frames in 1 key press like it's supposed to.

A: 

From what I can see the code you posted will just draw the last frame of your kick animation as the for loop in the LoopOnce method loops round all the frames and only draws the last one. This is because draw method is called once for each update.

XNA first calls the update method then after it calls draw. So in your update method your setting the current frame of the animation to the max frame, it then goes in to the draw method where you are then drawing the current frame which at this point is the last frame.

For this to work you would have to only increment the frame you want to draw by one for each call to update.

I don't have XNA on this computer so I cant test this and it probably wont compile without fixing any syntax errors but to give you an idea something like this should do it

private void UpdateAttack(KeyboardState current, KeyboardState last, GameTime gameTime)
        {
           if (current.IsKeyDown(Keys.S) && last.IsKeyUp(Keys.S))
           {
             mCurrentState =State.Kicking;
             currentImageIndex=25;
           }
           if(mCurrentState ==State.Kicking)
               neutralStandingKick(gameTime);              
        }

   private void neutralStandingKick(GameTime gameTime)
    {
           currentImageIndex++;
            if(currentImageIndex==30) mCurrentStart = State.SomeOtherState;
    } 

Remove your draw once method. You'll need to replace State.SomeOtherState with the state its in when the object is not kicking. You'll probably also want to swap the hard coded frame numbers with constants. Your draw method shouldn't need to be changed for this animation to work. Hope this helps.

Gavin Draper
Let me post the draw method that is part of this class up really quick. I'm a little new to asking XNA related questions. X_x;
Dave
Ok, I understand what you mean. I can't believe I missed this. I was thinking I could use the for loop do the kicking animation's X amount of frames. I forgot that the update method is only called once per loop, thus only one frame from the for loop getting passed to the draw method. Thank you for your help, I'll find a different approach to handling this!
Dave
Thank you very much Gavin. I will try this right now.
Dave
I've tried what you said and it would appear to work in theory but for some reason isn't working automatically. I'll post the modified code up. It should be looping a total of about 7 frames on 1 key press, but it's not. Edit: I can hold the keyboard button down and it will loop through the animation correctly, just not automatically.
Dave
I can see 2 ways this could be happening 1 - you are setting mCurrentState somewhere else. Maybe in your draw method? Something is setting the state to something other than kicking before the animation is over. 2 - Alternatively the problem is UpdateAttack is only being caled when the kick button is held down. Can you post the code that calls the UpdateAttack method.
Gavin Draper
Yep, let me get that up for ya.
Dave
Ok, I've found the problem now. It's working and you're correct about. I had aCurrentKeyboardState.Equals(new KeyboardState() placed in my State.Idle if statement, it was overriding all the other enum states. Now the problem is 100% solved. Thank you again Mr. Gavin!
Dave