Basic Sprite Class

From XNAWiki
Jump to: navigation, search

This is a super basic class to just hold all of the data for a sprite being drawn on the screen. I find it useful to do it this way because then it's much easier when it comes to drawing them. You just pass in a SpriteBatch and you're good to go.

public class Sprite
{
	public Texture2D Texture;
	public Vector2 Position = Vector2.Zero;
	public Rectangle? SourceRect = null;
	public Color Color = Color.White;
	public float Rotation = 0f;
	public Vector2 Origin = Vector2.Zero;
	public float Scale = 1f;
	public SpriteEffects Effects = SpriteEffects.None;
	public float LayerDepth = 0;
 
	public void Draw(SpriteBatch spriteBatch)
	{
		if (Texture == null)
			return;
 
		spriteBatch.Draw(
			Texture,
			Position,
			SourceRect,
			Color,
			Rotation,
			Origin,
			Scale,
			Effects,
			LayerDepth);
	}
}