Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 926925
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T19:46:09+00:00 2026-05-15T19:46:09+00:00

I’ve been working on this for quite some time looking for solutions in various

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-15T19:46:10+00:00Added an answer on May 15, 2026 at 7:46 pm

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a jquery bug and I've been looking for hours now, I can't
For some reason, after submitting a string like this Jack’s Spindle from a text
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have just tried to save a simple *.rtf file with some websites and
I used javascript for loading a picture on my website depending on which small
this is what i have right now Drawing an RSS feed into the php,
I have this code to decode numeric html entities to the UTF8 equivalent character.
I have this code: - (void)parser:(NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock { NSString *someString = [[NSString

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.