Hey,
In my program there is some bombs generating, but on start, after adding new one to list, it should set bomb’s parameters but throws “Object reference not set to an instance” error. . After a long time I still cant figure out the mistake.
Below there’s all of the relevant code.
Base class:
> namespace Monogame_SprDeathRespawn
{ public class Game1 : Game { GraphicsDeviceManager graphics; SpriteBatch spriteBatch;
public static int screenWidth; public static int screenHeight; public static Random random;
public List<SpriteClass> sprites= new List<SpriteClass>(); public bool hasStarted= false; public Texture2D bombTexture; private float timer; public Game1() { graphics = new GraphicsDeviceManager(this); Content.RootDirectory = "Content";
screenHeight = graphics.PreferredBackBufferHeight; screenWidth = graphics.PreferredBackBufferHeight; } protected override void Initialize() { base.Initialize(); } protected override void LoadContent() { spriteBatch = new SpriteBatch(GraphicsDevice); bombTexture = Content.Load<Texture2D>("bomb");
Restart(); }
private void Restart() { var playerTexture = Content.Load<Texture2D>("randomManSpr");
sprites = new List<SpriteClass> { new Player(playerTexture) { position = new Vector2(screenWidth / 2 - playerTexture.Width / 2, screenHeight - playerTexture.Height), input = new Input() { left = Keys.A, right = Keys.D }, speed = 10f } }; hasStarted = false; } protected override void UnloadContent() {
}
protected override void Update(GameTime gameTime) {
if (Keyboard.GetState().IsKeyDown(Keys.Space)) hasStarted = true;
if (hasStarted != true) return;
timer += (float)gameTime.ElapsedGameTime.TotalSeconds;
foreach (var sprite in sprites) { sprite.Update(gameTime, sprites); }
if (timer > 0.25f) { timer = 0; sprites.Add(new Bomb(Content.Load<Texture2D>("bomb"))); }
for (int i = 0; i < sprites.Count; i++) { var sprite = sprites[i]; if (sprite.isRemoved) { sprites.RemoveAt(i); i--; } if (sprite is Player) { var player = sprite as Player; if (player.hasDied) Restart(); } }
base.Update(gameTime); }
protected override void Draw(GameTime gameTime) { GraphicsDevice.Clear(Color.CornflowerBlue);
spriteBatch.Begin();
foreach (var sprite in sprites) { sprite.Draw(spriteBatch); }
spriteBatch.End();
base.Draw(gameTime); } }
}
Sprite class:
namespace Monogame_SprDeathRespawn.Sprite
{
public class SpriteClass
{
protected Texture2D texture;
public Vector2 position; public Vector2 velocity; public float speed; public Input input; public bool isRemoved = false;
public Rectangle rectangle { get { return new Rectangle((int)position.X, (int)position.Y, texture.Width, texture.Height); } }
public SpriteClass(Texture2D texture) { this.texture = texture; }
public virtual void Update(GameTime gameTime, List<SpriteClass> sprites) {
}
public virtual void Draw(SpriteBatch spriteBatch) { spriteBatch.Draw(texture, position, Color.White); } }
}
Bomb class derived from Sprite:
namespace Monogame_SprDeathRespawn.Sprite
{
class Bomb : SpriteClass
{
public Bomb(Texture2D texture)
: base(texture)
{
this.position = new Vector2(Game1.random.Next(0, Game1.screenWidth + texture.Width), -texture.Height); //throws “Object reference not set to an instance” exception
speed = Game1.random.Next(3, 10); //throws the exception if line above commented
}
public override void Update(GameTime gameTime, List<SpriteClass> sprites) { position.Y += speed;
if (rectangle.Bottom >= Game1.screenHeight) isRemoved = true; } }
}
Thanks for helping.