Game Creation with XNA/2D Development/Sprites

What are Sprites?
Sprites are two dimensional image.The best known sprite is the mouse pointer.

Sprites are not only used in 2D games but sprites are also used in 3D games for example,

for splash screens, menus, explosions and fire.These graphics based on the followed coordinate system.



Creating Sprites
Important to creating a Sprite you should know that the file can be bmp, png or jpg. Most suitable are painting programms for creating Sprites such as Adobe Photoshop. For animations sprite sheets are necessary. Individual animation steps must be arranged in tabular form in the file.



Add Sprites
add the image to the project right click on the content file


 * "add"
 * new element-->> bitmap -->> you can draw in visual studio your own bitmap graphic
 * existing element-->> ..select a graphic on your own data structure



Let's create a few Texture2D objects to store our images. Add the following two lines of code as instance variables to our game's main class:

load the images into our texture objects. In the LoadContent method, add the following lines of code:

Using SpriteBatch
SpriteBatch is the most important class of 2D drawing. The class contains methods for drawing sprite onto the screen. SpriteBatch have many usefull methods you can find all about these class by msdna libary.

The standard template of Visual Studio already has added a SpriteBatch object.

the instance variables in the main:

a reference to this SpriteBatch class in the LoadContent method:

method Draw-->important

drawing with SpriteBatch


 * SpriteBatch.Draw (Texture2D, Rectangle, Color);
 * SpriteBatch.Draw (Texture2D, Vector, Color);

more about SpriteBatch.Draw



Make Sprites smaller /bigger /semitransparent and/or rotate
SpriteBatch.Draw must be overloaded to reduce or enlarge or rotate or make transparent Sprites.

In the method spriteBatch.Draw we can give to a color value not only "Color.White" but also RGB and even an alpha value.

API:

SpriteBatch.Draw Methode (Texture2D, Vector2, Nullable, Color, Single, Vector2, Single, SpriteEffects, Single)

public void Draw (
 * Texture2D texture,
 * Vector2 position,
 * Nullable sourceRectangle,
 * Color color,======>//this value can have an alpha value for transparent
 * float rotation,====>//this value is the radius at which the graphic is rotate
 * Vector2 origin,===>//this value is the point at which the graphic is rotate
 * float scale,======>//this value is important to reduce or enlarge sprites
 * SpriteEffects effects,
 * float layerDepth

)

more about the parameters find here



Animated Sprites
First, make a sprite sheet in which a motion sequence is shown for example go, jump, bend, run ..



Next add a new class named AnimateSprite and add the follow variables.

The class consists of three methods: LoadGraphic (loading of the texture and set the variable), Update (for updating or moving animation) and Draw (to draw the sprite).

LoadGraphic

In this method, the entire variable and the texture are assigned.

Update

Here, the animation is updated.

Draw

Here is the current frame is drawn.

Using in Game

add Code to class Game1

main:

LoadContent:

Update:

Draw:

Drawing Textfonts
add the Font to the project right click on the content file


 * "add"
 * "new element.."
 * SpriteFont

This file is an XML file, in which font, font size, font effects (bold, italics, underline), letter spacing and characters to use are given.

From these data, XNA created the bitmap font. To use German characters have to set the end value to 255.

the instance variables in the main:

in the LoadContent method:

in the Draw method:



Authors
SuSchu -- Susan Schulze

Usefull Websites
http://www.xnadevelopment.com/tutorials.shtml

http://msdn.microsoft.com/en-us/library/bb203893.aspx

http://rbwhitaker.wikidot.com/2d-tutorials

http://www.xnamag.de/articles.php?cid=5