0

I have tried to load texture on the PlayerTank class constructor. But I have received NullReferenceException.

namespace BattleCityv1._5.Tanks
{
    class PlayerTank : Tank
    {
        public PlayerTank(int life, Point position)
        {
            texture = Content.Load<Texture2D>(@"images/PlayerTankSpriteSheet");
            Life = life;
            Rectangle = new Rectangle(position.X, position.Y, 32, 32);
            Source = new Rectangle(0, 0, Rectangle.Width, Rectangle.Height);
            type = "player1";
            Color = Color.White;
        }
    }
}
Mykola
  • 3,343
  • 6
  • 23
  • 39

2 Answers2

0

The variable Content is a ContentManager? Are these variables even declared? Your class "Tank" does not extend Microsoft.Xna.Framework.Game, does it?

Content is probably null. But Content is only supposed to be in your class that extends Microsoft.Xna.Framework.Game (wich will probably named BattleCityv1 or something)

What I suggest is getting your Textures from your class that extends Microsoft.Xna.Framework.Game. Make a static Dictionary, load all textures once, and get these textures by accessing that Dictionary. It's probably the most simple solution.

M.Hoepp
  • 3
  • 3
0

The texture object was never really created.

texture = ContentManager.Load<Texture2D>(@"images/PlayerTankSpriteSheet");

is what you typed. But Content.Load<> has the parameters (string assetName).

@"images/PlayerTankSpriteSheet"

is not an asset name. It is a file path. You need to click on the sprite in the Solution Explorer under Content, then find the asset name. This is what you type in as a parameter for the ContentManager.Load<>

Daniel G
  • 245
  • 4
  • 15