Dream come true XNA (4)-Animation

Source: Internet
Author: User

[Index page]
[Download source code]

Dream come true XNA (4)-Animation

Author: webabcd

Introduction
XNA: Animation

Example
1. Demo for Sprite Movement (press the F key on the keyboard to load this Demo)
Component/Sprite/Animation. cs

Using System; using System. collections. generic; using System. linq; using Microsoft. xna. framework; using Microsoft. xna. framework. audio; using Microsoft. xna. framework. content; using Microsoft. xna. framework. gamerServices; using Microsoft. xna. framework. graphics; using Microsoft. xna. framework. input; using Microsoft. xna. framework. media; namespace XNA. component. sprite {public class Animation: Microsoft. xna. framework. drawableGameComponent {// SpriteBatch _ spriteBatch; // sprite object Texture2D _ sprite; // describe the sprite position Vector2 _ spritePosition = Vector2.Zero; // describe the sprite speed: _ spriteSpeed. X indicates the number of frames that move to the X axis, _ spriteSpeed. Y indicates the number of frames that are moved to the Y axis. Vector2 _ spriteSpeed = new Vector2 (3, 10); public Animation (Game game): base (game) {} public override void Initialize () {base. initialize ();} protected override void LoadContent () {_ spriteBatch = new SpriteBatch (Game. graphicsDevice); _ sprite = Game. content. load <Texture2D> ("Image/Son");} public override void Update (GameTime gameTime) {// Update the sprite location _ spritePosition based on the speed. X + = _ spriteSpeed. x; _ spritePosition. Y + = _ spriteSpeed. y; // In the X axis direction, if the genie encounters the left and right boundary, the X axis speed changes to the opposite direction if (_ spritePosition. x> Game. window. clientBounds. width-_ sprite. width | _ spritePosition. X <0) _ spriteSpeed. X * =-1; // in the Y axis direction, if the sprite encounters the upper and lower boundary, the Y axis speed changes to the opposite direction if (_ spritePosition. y> Game. window. clientBounds. height-_ sprite. height | _ spritePosition. Y <0) _ spriteSpeed. Y * =-1; base. update (gameTime);} public override void Draw (GameTime gameTime) {Game. graphicsDevice. clear (Color. cornflowerBlue); // draws sprite _ spriteBatch at the specified position. begin (); _ spriteBatch. draw (_ sprite, _ spritePosition, Color. white); _ spriteBatch. end (); base. update (gameTime );}}}

2. Demo of Sprite animation through Sprite Sheet (press the G key on the keyboard to load this Demo)
Component/Sprite/AnimatingSprite. cs

/** For the Sprite Sheet diagram used in this example, see XNAContent/SpriteSheet/Smurf.png */using System; using System. collections. generic; using System. linq; using Microsoft. xna. framework; using Microsoft. xna. framework. audio; using Microsoft. xna. framework. content; using Microsoft. xna. framework. gamerServices; using Microsoft. xna. framework. graphics; using Microsoft. xna. framework. input; using Microsoft. xna. framework. media; namespace XNA. component. sprite {public class AnimatingSprite: Microsoft. xna. framework. drawableGameComponent {// SpriteBatch _ spriteBatch; // sprite object Texture2D _ Sprite; // The size Point _ frameSize = new Point (128,128) of each independent image in the sprite Sheet ); // The position of the current independent image in the Sprite Sheet: Point _ currentFrame = new Point (0, 0); // The number of rows and columns of the Sprite Sheet: Point _ sheetSize = new Point (4, 4); // The Frame Rate const int SPRITE_FPS = 10 of the sprite animation; // In the sprite animation, the current time distance shows the time experienced by the previous independent image. Unit: millisecond int _ elapsedTime = 0; public AnimatingSprite (Game game): base (game) {// Game. the default value of TargetElapsedTime is new TimeSpan (0, 0, 0, 0, 16), that is, approximately 60 fps (frames per second) // The method for modifying the frame rate is as follows, it means 1 frame per 100 milliseconds, that is, 10 frames per second // game. targetElapsedTime = new TimeSpan (0, 0, 0, 0,100);} public override void Initialize () {base. initialize ();} protected override void LoadContent () {_ spriteBatch = new SpriteBatch (Game. graphicsDevice); _ sprite = Game. content. load <Texture2D> ("SpriteSheet/Smurf");} public override void Update (GameTime gameTime) {/** GameTime-current game time object * GameTime. elapsedGameTime-the time the last Update () method was called * GameTime. isRunningSlowly-Indicates whether to skip frames. true indicates that XNA cannot keep up with the specified frame rate * GameTime. totalGameTime-total time that the game has experienced since the beginning */_ elapsedTime + = gameTime. elapsedGameTime. milliseconds; if (_ elapsedTime> 1000/SPRITE_FPS) {_ elapsedTime-= 1000/SPRITE_FPS; ++ _ currentFrame. x; if (_ currentFrame. x> = _ sheetSize. x) {_ currentFrame. X = 0; ++ _ currentFrame. y; if (_ currentFrame. Y <= _ sheetSize. y) {_ currentFrame. Y = 0 ;}} base. update (gameTime);} public override void Draw (GameTime gameTime) {Game. graphicsDevice. clear (Color. cornflowerBlue); _ spriteBatch. begin (); _ spriteBatch. draw (// sprite object _ sprite, // place the genie in the middle of the Game window new Vector2 (Game. window. clientBounds. width-_ frameSize. x)/2, (Game. window. clientBounds. height-_ frameSize. y)/2), // display the specified independent image in the Sprite Sheet new Rectangle (_ currentFrame. X * _ frameSize. x, _ currentFrame. Y * _ frameSize. y, _ frameSize. x, _ frameSize. y), // Color is not colored. white); _ spriteBatch. end (); base. update (gameTime );}}}

OK
[Download source code]

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.