【问题标题】:XNA Windows Game (4.0) ContentLoadExceptionXNA Windows 游戏 (4.0) ContentLoadException
【发布时间】:2012-10-31 01:45:31
【问题描述】:

其他人可以制作和编译这个程序吗(Click here) 直到第一次“快速构建”..?(直到他们说“这将是一个快速构建的好时机”)

由于某种原因,我不断收到此异常!

我已经检查了这个 Stackoverflow 解决方案中提到的所有内容:Click Here,但都没有解决我的问题 :(

我的图像被放置在内容区域。它不位于文件夹中。

请帮忙!

这是我的代码的样子:

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);
        }
    }
}

【问题讨论】:

  • 您的问题可能是您正在创建自己的 ContentManager。尝试使用:this.Content.Load("HealthBar")
  • 我试过这样做,但它并没有解决问题....还是谢谢
  • 如果您使用自己的 ContentManager,您还必须将 RootDirectory 属性设置为“Content”,或者如果您有不同的文件夹,则可以将其设置为。
  • 正如我在上面的评论中提到的,即使我不使用自己的 ContentManager,也存在同样的问题....
  • 我们需要知道异常中的消息,而不仅仅是类型。

标签: c# .net xna


【解决方案1】:

如果您完全按照教程进行操作,则您已将图像添加到游戏项目而不是内容项目中。这是由于遵循了一个非常过时的教程(1.0 与当前的 4.0)

右键单击内容项目,添加现有文件并添加图像。

作为旁注,我高度建议您 File>New 并在 http://www.riemers.net/ 上做教程 从 1.0 到 4.0 的代码破坏性更改太多,甚至无法尝试编写 1.0 教程。

【讨论】:

  • 您好,不,我已将其添加到内容项目中,而不是游戏项目中...无论如何,谢谢..
【解决方案2】:

如果您想使用自己的 Content Manager 目录,您需要创建一个 Content 项目并将其在属性中的根目录设置为您想要的任何内容,从 VS。这会将已编译资产的目标目录从默认内容更改为您选择的任何内容。您也可以在默认的 Content 项目中直接设置此值。

然后,在您自己的实例化时,指定根目录并照常加载资产,使用相对路径。它将在您选择的根目录中查找它们。

【讨论】:

  • 感谢您的回复,这不是我想要的。不过还是谢谢
猜你喜欢
  • 1970-01-01
  • 2014-01-25
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多