Drawing multiple sprites in XNA

With the trial of my Torque X running out I decided to do things the classic way. If you are starting out in XNA like me I really recommend this. It is a much better way of learning. Starting off with a game engine was hiding a lot away from me. Like drawing sprites onto the screen. Torque X handles drawing and movement for you without the need for you to write any code.

Now that I have to draw my own sprites I had two choices a DrawableGameComponent or a loop in the draw method of the game1 class to load all my sprites. I went with the second approach.  A modified version of my sprite class is below:

public class BoardPiece
{
   public static Texture2D Piece1;
   public static Texture2D Piece2;
   public Vector2 Position;
   public int PieceType;
 
   public BoardPiece(Vector2 newLoc, int pieceType)
   {
      Position = newLoc;
      PieceType = pieceType;
   }
 
   internal static void LoadTextures(ContentManager Content)
   {
      Piece1 = Content.Load<Texture2D>("images\\piece1");
      Piece2 = Content.Load<Texture2D>("images\\piece2");
   }
 
   public void Draw(SpriteBatch spriteBatch)
   {
      spriteBatch.Begin(SpriteBlendMode.AlphaBlend);
      if (PieceType == 1)
      {
         spriteBatch.Draw(Piece1, Position, Color.White);
      }
      else if (PieceType == 2)
      {
         spriteBatch.Draw(Piece2, Position, Color.White);
      }
      spriteBatch.End();
   }
 
}

in the Game1 class I have an array of BoardPieces  that are looped  and drawn out in the Draw method. e.g. piece[i].Draw(spriteBatch).

Next I’ll be moving onto selection of sprites using the gamepad…

2 Responses to “Drawing multiple sprites in XNA”

  1. You should try and get tabbing on these code posts.

    Also, wheres an obligatory screenshot? :)

  2. admin says:

    Thank you for the comment. I’ve formatted the code a bit better. I will upload screenshots once the game has been a bit polished.

Leave a Reply