X hits on this document

235 views

0 shares

0 downloads

0 comments

74 / 102

           // Remember that updating "currentFrame" will also

           // update the FrameIndex property.

           currentFrame.X++;

           if (currentFrame.X >= sheetSize.X)

           {

               currentFrame.X = 0;

               currentFrame.Y++;

           }

           if (currentFrame.Y >= sheetSize.Y)

               currentFrame.Y = 0;

       }

   }

}

The above method simply advances the animation by a single frame, stopping the animation if it has reached the final frame.

160.

Add the following to Draw methods to the animation class:

C#

public void Draw(SpriteBatch spriteBatch, Vector2 position, SpriteEffects spriteEffect)

{

   Draw(spriteBatch, position, 1.0f, spriteEffect);

}

public void Draw(SpriteBatch spriteBatch, Vector2 position, float scale, SpriteEffects spriteEffect)

{

   spriteBatch.Draw(animatedCharacter, position + Offset, new Rectangle(

           frameSize.X * currentFrame.X,

           frameSize.Y * currentFrame.Y,

           frameSize.X,

           frameSize.Y),

           Color.White, 0f, Vector2.Zero, scale, spriteEffect, 0);

}

The above methods simply draw the portion of the animation sheet that matches the current frame, with the second override allowing the animation to be scaled.

161.

Add a final method to the Animation class and name it PlayFromFrameIndex. This method will be used for playing an animation from a specified frame.

C#

public void PlayFromFrameIndex(int frameIndex)

{

   FrameIndex = frameIndex;

   IsActive = true;

Page | 74

Document info
Document views235
Page views235
Page last viewedMon Dec 05 01:59:26 UTC 2016
Pages102
Paragraphs3115
Words17333

Comments