简体   繁体   中英

XNA Windows Game (4.0) ContentLoadException

Can anyone else make and compile this program( Click here) up until the first "quick build"..? (right up to the point where they say "This would be a great time to do a quick build")

For some reason i keep getting this exception!

I've checked everything mentioned in this Stackoverflow solution: Click Here but none of it has solved my issue :(

my image is placed in the Content area. It is not located in a folder.

Please help!

here is what my code looks like:

namespace myGame
{

    public class Game1 : Microsoft.Xna.Framework.Game
    {
        GraphicsDeviceManager graphics;
        SpriteBatch spriteBatch;

        SpriteBatch mBatch;
        Texture2D mHealthBar;

        public Game1()
        {
            graphics = new GraphicsDeviceManager(this);
            Content.RootDirectory = "Content";
        }





        protected override void Initialize()
        {
            // TODO: Add your initialization logic here

            base.Initialize();
        }





        protected override void LoadContent()
        {

            spriteBatch = new SpriteBatch(GraphicsDevice);

            // TODO: use this.Content to load your game content here
            mBatch = new SpriteBatch(this.graphics.GraphicsDevice);
            ContentManager aLoader = new ContentManager(this.Services);

            **//ERROR occurs here!**
            mHealthBar = aLoader.Load<Texture2D>("HealthBar") as Texture2D;
        }




        protected override void UnloadContent()
        {
            // TODO: Unload any non ContentManager content here
        }




        protected override void Update(GameTime gameTime)
        {
            // Allows the game to exit
            if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed)
                this.Exit();

            // TODO: Add your update logic here

            base.Update(gameTime);
        }





        protected override void Draw(GameTime gameTime)
        {
            GraphicsDevice.Clear(Color.CornflowerBlue);

            // TODO: Add your drawing code here
            mBatch.Begin();
            mBatch.Draw(mHealthBar, new Rectangle(this.Window.ClientBounds.Width / 2 - mHealthBar.Width / 2,

                 30, mHealthBar.Width, 44), new Rectangle(0, 45, mHealthBar.Width, 44), Color.Red);


            //Draw the box around the health bar
            mBatch.Draw(mHealthBar, new Rectangle(this.Window.ClientBounds.Width / 2 - mHealthBar.Width / 2,
                  30, mHealthBar.Width, 44), new Rectangle(0, 0, mHealthBar.Width, 44), Color.White);
            mBatch.End(); 
            base.Draw(gameTime);
        }
    }
}

If you followed the tutorial precisely, you have added the image to the game project instead of the content project. This is a result of following a vastly outdated tutorial (1.0 vs the current 4.0)

Right click on the Content Project, add existing file and add the image.

As a side note, I HIGHLY suggest you File>New and do the tutorials at http://www.riemers.net/ There are too many code breaking changes from 1.0 to 4.0 to even attempt to do a 1.0 tutorial.

If you want to use your own Content Manager directory you need to create a Content project and set its root directory in the properties to whatever you want, from VS. This will change the target dir for compiled assets from the default Content to whatever you chose. You can directly set this value in the default Content project, too.

Then, when instancing your own, specify the Root Directory and load the assets as usual, with the relative path. It will look for them in your selected root.

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