Note that there are some explanatory texts on larger screens.

plurals
  1. POXNA Fighting Game Style Animation Looping Issue
    primarykey
    data
    text
    <p>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.</p> <p>Here is my code: </p> <pre class="lang-c# prettyprint-override"><code>private void UpdateAttack(KeyboardState current, KeyboardState last, GameTime gameTime) { if (current.IsKeyDown(Keys.S) &amp;&amp; last.IsKeyUp(Keys.S)) { neutralStandingKick(gameTime); } } private void neutralStandingKick(GameTime gameTime) { //timeSinceLastFrame += gameTime.ElapsedGameTime.Milliseconds; //Framerate control //if (timeSinceLastFrame &gt; 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 &gt; max || currentImageIndex &lt; 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 &lt; max; i++) //Uses the range to determine when to stop { currentImageIndex++; } //Increments index each iteration that passes } </code></pre> <p>Edit: Here is the Draw method of this particular class</p> <pre class="lang-c# prettyprint-override"><code>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 } </code></pre> <p>Why wouldn't this work?</p> <p>New Code (Gavin's Suggestion): </p> <pre class="lang-c# prettyprint-override"><code>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) &amp;&amp; mCurrentState != State.Kicking) { mCurrentState = State.Kicking; currentImageIndex = min; } if (mCurrentState == State.Kicking) { if (timeSinceLastFrame &gt; millisecondsPerFrame) //Framerate control { timeSinceLastFrame -= millisecondsPerFrame; //Framerate control currentImageIndex++; } } if (currentImageIndex == max) mCurrentState = State.Idle; } </code></pre> <p>Method that calls UpdateAttack:</p> <pre class="lang-c# prettyprint-override"><code>public void Update(GameTime theGameTime, Game game) { KeyboardState aCurrentKeyboardState = Keyboard.GetState(); UpdateMovement(aCurrentKeyboardState); UpdateJump(aCurrentKeyboardState); UpdateAttack(aCurrentKeyboardState, mPreviousKeyboardState, theGameTime); UpdateStageBounds(game); mPreviousKeyboardState = aCurrentKeyboardState; } </code></pre> <p>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. </p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload