This page is optimized for mobile devices, if you would prefer the desktop version just click here

0.12 Xna0122-frame animation using a sprite sheet  (Page 3/12)

Figure 2 . A top row image.

Figure 3 . A bottom row image.

Figure 4 . A bottom row image flipped horizontally.

Figure 5 . A top row image flipped horizontally.

You should be able to correlate the images of the dog shown in Figure 2 through Figure 5 with the individual images shown in Figure 1 . Note, however, that the images in Figure 4 and Figure 5 were flipped horizontally so that the dog would be facing the correctway when moving from left to right across the game window.

Discussion and sample code

I will explain the code in this program in fragments, and I will only discuss the code that I modified relative to the skeleton code produced by Visual C#when I created the project. A complete listing of the file named Game1.cs is shown in Listing 14 near the end of the module.

The class named Game1

The class named Game1 begins in Listing 1 .

Listing 1 . Beginning of the class named Game1.

namespace XNA0122Proj { public class Game1 : Microsoft.Xna.Framework.Game {//Declare and populate instance variablesGraphicsDeviceManager graphics; SpriteBatch spriteBatch;Texture2D myTexture; Vector2 spritePosition = new Vector2(0.0f,0.0f);int slide = 8;//Used to move sprite across screen. int scale = 4;//Size scale factor.int fast = 175;//Used for fast frame rate. int slow = 525;//Used for slow frame rate.int msPerFrame = 0;//Gets set for fast or slow. int msElapsed;//Time since last new frame.int spriteCol;//Sprite column counter. int spriteColLim = 5;//Number of sprite columns.int spriteRow;//Sprite row counter. int spriteRowLim = 2;//Number of sprite rows.int frameWidth;//Width of an individual image int frameHeight;//Height of an individual imageint xStart;//Corner of frame rectangle int yStart;//Corner of frame rectangleSpriteEffects noEffect = SpriteEffects.None; SpriteEffects flipEffect =SpriteEffects.FlipHorizontally; SpriteEffects spriteEffect;//noEffect or flipEffectint winWidth;//Width of the game window. int funSequenceCnt = 0;int pauseSequenceCnt = 0;

Listing 1 declares a large number of instance variables that are used by code throughout the program. I will explain the purpose of the instance variableswhen we encounter them in the program code later.

The modified constructor for the Game1 class

The constructor for the Game1 class is shown in Listing 2 .

Listing 2 . The constructor for the Game1 class.

public Game1() {//constructor graphics = new GraphicsDeviceManager(this);Content.RootDirectory = "Content"; //Set the size of the game window.graphics.PreferredBackBufferWidth = 450; graphics.PreferredBackBufferHeight = 100;}// end constructor

I added the last two statements to the standard constructor that is generated by Visual C# when you create a new project.

Although it isn't very clear in the documentation , the values for PreferredBackBufferWidth and PreferredBackBufferHeight set the size of the game window provided that they are consistent with the screen resolution. These twostatements caused the game window to be small as shown in Figure 2 instead of the default size.

<< Chapter < Page Page > Chapter >>

Read also:

OpenStax, Xna game studio. OpenStax CNX. Feb 28, 2014 Download for free at https://legacy.cnx.org/content/col11634/1.6
Google Play and the Google Play logo are trademarks of Google Inc.
Jobilize.com uses cookies to ensure that you get the best experience. By continuing to use Jobilize.com web-site, you agree to the Terms of Use and Privacy Policy.