ArtOfCode ArtOfCode - 3 months ago 12
C# Question

How do I properly reference Content in Visual Studio 2010 with XNA?

I'm making a C# game using Visual C# Studio 2010 Express with Microsoft's XNA.

At the moment I'm trying to load content into the game but I'm having trouble with the relative Content path. My current code looks like this:

private Texture2D planetBackground;
private Texture2D groundFacility;
private Texture2D hoverShip;
private Texture2D attackShip;

protected override void LoadContent()
{
spriteBatch = new SpriteBatch(GraphicsDevice);
planetBackground = Content.Load<Texture2D>("spacebackground.png");
groundFacility = Content.Load<Texture2D>("planetstation.png");
hoverShip = Content.Load<Texture2D>("ship2.png");
attackShip = Content.Load<Texture2D>("ship1.png");
}


The
Content.RootDirectory
is currently set to "Content".

How do I construct the paths to the images so that they will load? At the moment I get a
ContentLoadException: file not found
, so clearly my relative path is wrong. Where does the path start from?

Answer

Remove file extensions:

planetBackground = Content.Load<Texture2D>("spacebackground");

XNA transforms all files into .XNB files, so there's no need to specify extensions (unless your files have multiple dots in file names).

Comments