XNA Game-Themed CS1 Examples (XGC1) | |
Release 3.0 (XNA V4) |
Reference: This is the fifth tutorial on how to work with the XNACS1Lib library. It is assumed you have read and understand the previous four tutorials:
Goals : This tutorial concentrates on describing how to work with Sprite Sheets:
1. Obtain the example code
2. The Source Code Files/Structure
Take a look at the ExampleProgram folder that you have unzipped and you will see a structure identicle to that of the previous tutorials.
3. The Solution Explorer:
Now double click on the
*.sln
file in the
ExampleProgram
folder, to start the
project in the IDE. Notice the structure of the
SolutionExplorer
follows the source code folder structure we have examined:
4. Examine the Predefined Emitters:
We are finally ready to examine our source code for Sprite Sheets. To declare a Spirte Sheet use the following code:
public
class Game1 :
XNACS1Base { XNACS1Rectangle protected override void InitializeWorld() { Sprite = new XNACS1Rectangle(new Vector2(50,25), 10, 10); Sprite.SetTextureSpriteSheet("Sprites", 13, 16, 0); Sprite.UseSpriteSheet = true ; Sprite.SetTextureSpriteAnimationFrames(0, 0, 1, 0, 5, SpriteSheetAnimationMode.AnimateForward); Sprite.UseSpriteSheetAnimation = true; } |
The XNACS1Rectangle.SetTextureSpriteSheet(String SpriteSheetTexture, int numColumns, int numRows, int padding) arguments are pretty straightforward.
You may also notice the lines of code, "Sprite.SetTextureSpriteAnimationFrames(0, 0, 1, 0, 5, SpriteSheetAnimationMode.AnimateForward);" and "Sprite.SetTextureSpriteAnimationFrames(0, 0, 1, 0, 5, SpriteSheetAnimationMode.AnimateForward);" What the first line of code does is set the current animation of your Sprite object, to a sprite from the sprite sheet. The second line of code enables the Sprite object to loop through animiations if you want.
The XNACS1Rectangle.SetTextureSpriteAnimationFrames(int beginX, int beginY, int endX, int endY, int ticksPerFrame, SpriteSheetAnimationMode mode) arguments are a little more confusing.
The following is the code that causes the sprite to change directions when a direction on the dpad is pressed. It is one example of how you can use the sprite sheet animations.
protected
override void
UpdateWorld() { if (GamePad.Dpad.Up == ButtonState.Pressed && (Sprite.CurrentSpriteSheetFrameX < 0 || Sprite.CurrentSpriteSheetFrameX > 1)) Sprite.SetTextureSpriteAnimationFrames(0, 0, 1, 0, 5, SpriteSheetAnimationMode.AnimateForward); else if (GamePad.Dpad.Down == ButtonState.Pressed && (Sprite.CurrentSpriteSheetFrameX < 3 || Sprite.CurrentSpriteSheetFrameX > 4)) Sprite.SetTextureSpriteAnimationFrames(3, 0, 4, 0, 5, SpriteSheetAnimationMode.AnimateForward); else if (GamePad.Dpad.Left == ButtonState.Pressed && (Sprite.CurrentSpriteSheetFrameX < 6 || Sprite.CurrentSpriteSheetFrameX > 7)) Sprite.SetTextureSpriteAnimationFrames(6, 0, 7, 0, 5, SpriteSheetAnimationMode.AnimateForward); else if (GamePad.Dpad.Right == ButtonState.Pressed && (Sprite.CurrentSpriteSheetFrameX < 9 || Sprite.CurrentSpriteSheetFrameX > 10)) Sprite.SetTextureSpriteAnimationFrames(9, 0, 10, 0, 5, SpriteSheetAnimationMode.AnimateForward); } |
Kelvin Sung Computing and Software Systems University of Washington, Bothell ksung@u.washington.edu |
Michael Panitz Business And Information Technology Cascadia Community College mpanitz@cascadia.eduu |
This work is supported in part by a grant from Microsoft Research under the Computer Gaming Curriculum in Computer Science RFP, Award Number 15871 and 16531. | |