Click here to Skip to main content
15,881,803 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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!

C#
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;

		//The size of the game window
		const int windowWidth = 800;
		const int windowHeight = 600;

		Texture2D Background;
		Texture2D Pieces;

		//Call, but do not create a new GameBoard
		GameBoard gameBoard;

		//Creates spaces where the game pieces will go
		public Rectangle EmptyPiece = new Rectangle(0, 288, 48, 48);

		//The size in pixels for the water graphic
		public const int MaxWaterHeight = 192;
		public const int WaterWidth = 192;

		//The position of the filled water image
		public Vector2 waterOverlayStart = new Vector2(112, 320);

		//The location where the water will be drawn
		public Vector2 waterPosition = new Vector2(576, 336);

		//Stores information about current flood
		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()
		{
			//Red
			spriteBatch.Draw(Background,
				new Rectangle(0, 0,
					windowWidth, windowHeight),
				Color.White);
		}

		public void DrawAnimatedWater()
		{
			int waterHeight = (int)(MaxWaterHeight * (floodCount / 100));

			//Animate the Red water
			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);
		}

		//Draws an empty piece at a specific x and y location
		public void DrawEmptyPiece(int pixelX, int pixelY)
		{
			//Red
			spriteBatch.Draw(
				Pieces,
				new Rectangle(pixelX, pixelY,
					GamePiece.PieceWidth, GamePiece.PieceHeight),
				EmptyPiece, Color.White);
		}

		//Draws static unanimated piece at a specific x and y location
		public void DrawStandardPiece(int x, int y, int pixelX, int pixelY)
		{
			//Red
			spriteBatch.Draw(
				Pieces, new Rectangle(pixelX, pixelY,
					GamePiece.PieceWidth, GamePiece.PieceHeight),
				gameBoard.GetSourceRect(x, y),
				Color.White);
		}

		//Draws a falling piece at a specific x and y location
		public void DrawFallingPiece(int pixelX, int pixelY, string positionName)
		{
			//Red
			spriteBatch.Draw(
				Pieces,
				new Rectangle(pixelX, pixelY -
					gameBoard.fallingPieces[positionName].VerticalOffset,
					GamePiece.PieceWidth, GamePiece.PieceHeight),
					gameBoard.fallingPieces[positionName].GetSourceRect(),
					Color.White);
		}

		//Draws a fading piece at a specific x and y location
		public void DrawFadingPiece(int pixelX, int pixelY, string positionName)
		{
			//Red
			spriteBatch.Draw(
				Pieces,
				new Rectangle(pixelX, pixelY,
					GamePiece.PieceWidth, GamePiece.PieceHeight),
					gameBoard.fadingPieces[positionName].GetSourceRect(),
					Color.White *
					gameBoard.fadingPieces[positionName].alphaLevel);
		}

		//Draws a rotating piece at a specific x and y location
		public void DrawRotatingPiece(int pixelX, int pixelY, string positionName)
		{
			//Red
			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>
Posted
Updated 18-Oct-11 17:30pm
v5
Comments
Sergey Alexandrovich Kryukov 21-Sep-11 20:59pm    
Any particular problem?
--SA
Pravin Patil, Mumbai 22-Sep-11 0:47am    
This is too much code, please explain in simple languange what exactly is your problem.....?

1 solution

Uses a debugger (and set it to break on exception). That way, you will know where the problem is as soon as it occurs.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900