Time for action – building a GamePiece class: constructors

  1. Add two constructors to your GamePiece.cs file after the declarations:
      public GamePiece(string type, string suffix)
      {
          pieceType = type;
          pieceSuffix = suffix;
      }
    
      public GamePiece(string type)
      {
          pieceType = type;
          pieceSuffix = "";
      }

What just happened?

A constructor is run when an instance of the GamePiece class is created. By specifying two constructors, we will allow future code to create a GamePiece by specifying a piece type with or without a suffix. If no suffix is specified, an empty suffix is assumed.

Updating a GamePiece

When a GamePiece is updated, you can change the piece type, the suffix, or both.

Get XNA 4.0 Game Development by Example Beginner's Guide now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.