I encapsulated all of my drawing methods into separate classes - one for each color. The color is supposed to change after every level. For example, Level 1 would be red, Lvl. 2 orange, Lvl. 3 amber (gold) and so on.
I have no errors in my Game1 class or my Draw_Red, Draw_Orange... classes, but whenever I run the game, I get a NullReferenceException Unhandled error.
Rather than inheriting all Game1 variables, which is what I was doing in my old code, I copied and pasted any variables relative to drawing into the color classes. I also added a LoadContent() method so the game would load the graphics after calling on the class. I don't know what I'm missing to get a NullReferenceException, but I'm starting to wonder if I need "SpriteBatch spriteBatch" in the parentheses of all my Draw() methods. If I do, why do I need to call the SpriteBatch class in at the top of my code? Any insight would be awesome!
Thanks!
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Content;
namespace Isaak_FloodControl
{
class Draw_Red:Microsoft.Xna.Framework.Game
{
SpriteBatch spriteBatch;
const int windowWidth = 800;
const int windowHeight = 600;
Texture2D Background;
Texture2D Pieces;
GameBoard gameBoard;
public Rectangle EmptyPiece = new Rectangle(0, 288, 48, 48);
public const int MaxWaterHeight = 192;
public const int WaterWidth = 192;
public Vector2 waterOverlayStart = new Vector2(112, 320);
public Vector2 waterPosition = new Vector2(576, 336);
float floodCount = 0.0f;
public void LoadContent(ContentManager myContent)
{
spriteBatch = new SpriteBatch(GraphicsDevice);
Background = myContent.Load<texture2d>
(@"Textures\Backgrounds\Bkg_Red");
Pieces = myContent.Load<texture2d>
(@"Textures\Tile_Sheets\Ts_Red");
}
public void DrawBackground()
{
spriteBatch.Draw(Background,
new Rectangle(0, 0,
windowWidth, windowHeight),
Color.White);
}
public void DrawAnimatedWater()
{
int waterHeight = (int)(MaxWaterHeight * (floodCount / 100));
spriteBatch.Draw(Background,
new Rectangle(
(int)waterPosition.X,
(int)waterPosition.Y + (MaxWaterHeight - waterHeight),
WaterWidth, waterHeight),
new Rectangle(
(int)waterOverlayStart.X,
(int)waterOverlayStart.Y + (MaxWaterHeight - waterHeight),
WaterWidth, waterHeight), Color.White);
}
public void DrawEmptyPiece(int pixelX, int pixelY)
{
spriteBatch.Draw(
Pieces,
new Rectangle(pixelX, pixelY,
GamePiece.PieceWidth, GamePiece.PieceHeight),
EmptyPiece, Color.White);
}
public void DrawStandardPiece(int x, int y, int pixelX, int pixelY)
{
spriteBatch.Draw(
Pieces, new Rectangle(pixelX, pixelY,
GamePiece.PieceWidth, GamePiece.PieceHeight),
gameBoard.GetSourceRect(x, y),
Color.White);
}
public void DrawFallingPiece(int pixelX, int pixelY, string positionName)
{
spriteBatch.Draw(
Pieces,
new Rectangle(pixelX, pixelY -
gameBoard.fallingPieces[positionName].VerticalOffset,
GamePiece.PieceWidth, GamePiece.PieceHeight),
gameBoard.fallingPieces[positionName].GetSourceRect(),
Color.White);
}
public void DrawFadingPiece(int pixelX, int pixelY, string positionName)
{
spriteBatch.Draw(
Pieces,
new Rectangle(pixelX, pixelY,
GamePiece.PieceWidth, GamePiece.PieceHeight),
gameBoard.fadingPieces[positionName].GetSourceRect(),
Color.White *
gameBoard.fadingPieces[positionName].alphaLevel);
}
public void DrawRotatingPiece(int pixelX, int pixelY, string positionName)
{
spriteBatch.Draw(
Pieces,
new Rectangle(pixelX + (GamePiece.PieceWidth / 2),
pixelY + (GamePiece.PieceHeight / 2),
GamePiece.PieceWidth, GamePiece.PieceHeight),
gameBoard.rotatingPieces[positionName].GetSourceRect(),
Color.White,
gameBoard.rotatingPieces[positionName].RotationAmount,
new Vector2(GamePiece.PieceWidth / 2,
GamePiece.PieceHeight / 2),
SpriteEffects.None, 0.0f);
}
}
}
</texture2d></texture2d>