2011-12-28 121 views
1

任何人都可以制作和编译这个程序(Click here))直到第一个“快速构建”。?(直到他们说“这将是一个伟大的时间来做一个!快建“)XNA Windows游戏(4.0)ContentLoadException

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

我检查这个#1方案中提到的一切: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); 
     } 
    } 
} 
+0

您的问题*可能是您正在创建自己的ContentManager。尝试使用:this.Content.Load (“HealthBar”) – GGulati 2011-12-28 01:45:30

+0

我试过这样做,但它没有解决问题....感谢反正 – BigBug 2011-12-28 04:03:34

+0

如果您使用自己的ContentManager,您必须将RootDirectory属性设置为“内容“,或者如果你有不同的文件夹,那么你可以将其设置为。 – annonymously 2011-12-28 05:06:10

回答

1

如果您正确地按照教程进行操作,您已将图像添加到游戏项目而不是内容项目。这是一个大大过时的教程(1.0与当前的4.0)的结果

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

作为一个方面说明,我HIGHLY建议你文件>新建并做教程在http://www.riemers.net/ 有太多的密码破译的变化,从1.0到4.0,甚至试图做一个1.0教程。

+0

你好,不,我已经将它添加到内容项目,而不是游戏项目......谢谢无论如何.. – BigBug 2011-12-30 06:39:33

+0

此外,感谢您的链接。我会确保检查出来.. – BigBug 2011-12-30 06:40:20

0

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

然后,在实例化自己的时候,指定根目录并像平常一样加载资源,并使用相对路径。它会在你选择的根目录中查找它们。

+0

感谢您的回复,并不是我一直在寻找的东西。但是,无论如何感谢 – BigBug 2011-12-31 07:59:34