I'm currently trying to set up a class to draw tiles for me in different shapes, but it's currently giving me a hard time, as it's giving me nullreferenceobject errors in my secondary class. My Game class:
public class Game1 : Microsoft.Xna.Framework.Game
{
GraphicsDeviceManager graphics;
SpriteBatch spriteBatch;
Tile tl = new Tile();
public Game1()
{
graphics = new GraphicsDeviceManager(this);
Content.RootDirectory = "Content";
graphics.PreferredBackBufferWidth = 1280;
graphics.PreferredBackBufferHeight = 960;
//graphics.IsFullScreen = true;
}
protected override void Initialize()
{
// TODO: Add your initialization logic here
base.Initialize();
}
protected override void LoadContent()
{
spriteBatch = new SpriteBatch(GraphicsDevice);
Content.RootDirectory = "Content";
}
protected override void UnloadContent()
{
// TODO: Unload any non ContentManager content here
}
protected override void Update(GameTime gameTime)
{
// Allows the game to exit
if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed)
this.Exit();
// TODO: Add your update logic here
base.Update(gameTime);
}
/// <summary>
/// This is called when the game should draw itself.
/// </summary>
/// <param name="gameTime">Provides a snapshot of timing values.</param>
///
inputForm iForm = new inputForm();
bool[,] centerPiece = new bool[,] { { false, true, false }, { true, true, true }, { false, true, false } };
bool[,] cornerPiece = new bool[,] { { false, true, false }, { true, true, false }, { false, false, false } };
bool[,] stairPiece = new bool[,] { { false, true, false }, { true, true, true }, { false, false, false } };
bool[,] straightPiece = new bool[,] { { false, true, false }, { false, true, false }, { false, true, false } };
protected override void Draw(GameTime gameTime)
{
GraphicsDevice.Clear(Color.White);
// Draw the sprite.
spriteBatch.Begin(SpriteSortMode.BackToFront, BlendState.AlphaBlend);
//Draw centerPiece
tl.centerPiece(spriteBatch, centerPiece);
spriteBatch.End();
base.Draw(gameTime);
//iForm.Show();
}
}
And my tile class:
public class Tile : Microsoft.Xna.Framework.Game
{
GraphicsDeviceManager graphics;
public float spacing = 32f;
public Texture2D squareTex;
public Vector2 IMGCenter()
{
Vector2 imageCenter = new Vector2(4 / 2f, 4 / 2f);
return imageCenter;
}
public Vector2 screenView()
{
Viewport viewport = GraphicsDevice.Viewport;
Vector2 screenCenter = new Vector2(viewport.Width / 2f, viewport.Height / 2f);
return screenCenter;
}
public Tile()
{
graphics = new GraphicsDeviceManager(this);
Content.RootDirectory = "Content";
squareTex = Content.Load<Texture2D>("square");
}
public void centerPiece(SpriteBatch batch, bool[,] squareCombo)
{
for (int x = 0; x < squareCombo.GetLength(0); x++)
{
for (int y = 0; y < squareCombo.GetLength(1); y++)
{
if (squareCombo[x, y] == true)
{
Vector2 offset = new Vector2(x, y) * spacing;
Vector2 position = IMGCenter() + offset;
batch.Draw(squareTex, screenView(), null, Color.White, 0f, position, 1f, SpriteEffects.None, 0f);
}
}
}
}
}
EDIT2: My current error:
An unhandled exception of type 'Microsoft.Xna.Framework.Content.ContentLoadException' occured in Microsoft.Xna.Framework.Graphics.dll.
Additional information: Error loading 'square' GraphicsDevice component not found.
NullReferenceException
so we can see where exactly it gets thrown. – Philipp Oct 25 at 9:27