Alright I'm stumped here, I can usually figure out most bugs, through use of Google or common sense but this one has me stumped!
I'm trying to load textures to an array of enemies (Like a row in space invaders) within my struct, however when I go to debug it throws a Null Reference, I know this means that I'm trying to access something that isn't there, I believe it's because i am calling this:
Inside Initialize I call my method:
Targets.setupSprite(this);
This is the actual method from inside the struct:
public void setupSprite(Game1 g)
{
widthFactor = 0.05f;
rowSize = 15;
spriteSpacingY = spriteRectangle.Height + 5;
for (int i = 0; i < rowSize; i++)
{
if (i < 15)
spriteSpacing = 10;
sprites[i].spriteTexture = g.texture;
x = 0 + (i * spriteSpacing);
y = spriteSpacingY;
visible = true;
}
}
Before the Textures can be setup by Content Loader. It recommends using 'new' but I'm not sure how to use it here.
The line that throws the error as you may have guessed is:
sprites[i].spriteTexture = g.texture;
g.texture is the texture for the enemy and is located in load content.
Any help would be greatly appreciated!
Additional Code:
protected override void Initialize()
{
displayHeight = GraphicsDevice.Viewport.Height;
displayWidth = GraphicsDevice.Viewport.Width;
new TargetRowStruct();
BreadBat.setupSprite(this);
CheeseBall.setupSprite(this);
Targets.setupSprite(this);
base.Initialize();
}
protected override void LoadContent()
{
// Create a new SpriteBatch, which can be used to draw textures.
spriteBatch = new SpriteBatch(GraphicsDevice);
texture = this.Content.Load<Texture2D>("Handsome");
swag = this.Content.Load<Texture2D>("swag");
back = this.Content.Load<Texture2D>("Backdrop");
font = this.Content.Load<SpriteFont>("FontA");
backRect.Width = (int)displayWidth;
backRect.Height = (int)displayHeight;
BreadBat.loadTexture(this);
CheeseBall.loadTexture(this);
Targets.loadTexture(this);
BreadBat.scaleSprites(this);
CheeseBall.scaleSprites(this);
Targets.scaleSprites(this);
}
Relevant Code within Targets Struct:
public void scaleSprites(Game1 g)
{
for (int i = 0; i < rowSize; i++)
{
sprites[i].spriteRectangle.Width = (int)((g.displayWidth * sprites[i].widthFactor) + .25f);
sprites[i].aspectRatio = (int)(sprites[i].spriteTexture.Width / sprites[i].spriteTexture.Height);
sprites[i].spriteRectangle.Height = sprites[i].spriteRectangle.Width;
}
}
public void loadTexture(Game1 g)
{
for (int i = 0; i < rowSize; i++)
{
sprites[i].spriteTexture = g.texture;
}
}
public void drawSprite(Game1 g)
{
g.spriteBatch.Draw(spriteTexture, spriteRectangle, Color.White);
for (int i = 0; i < rowSize; i++)
{
if (sprites[i].visible)
{
g.spriteBatch.Draw(sprites[i].spriteTexture, sprites[i].spriteRectangle, Color.White);
}
}
}
public void setupSprite(Game1 g)
{
widthFactor = 0.05f;
rowSize = 15;
spriteSpacingY = spriteRectangle.Height + 5;
for (int i = 0; i < rowSize; i++)
{
if (i < 15)
spriteSpacing = 10;
x = 0 + (i * spriteSpacing);
y = spriteSpacingY;
visible = true;
}
}
P.s It should be noted that the bread and cheese struct which are almost identical (except for being one sprite instead of an array) work fine.