简体   繁体   中英

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?

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).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM