任何人都可以制作和编译这个程序(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);
}
}
}
您的问题*可能是您正在创建自己的ContentManager。尝试使用:this.Content.Load(“HealthBar”) –
GGulati
2011-12-28 01:45:30
我试过这样做,但它没有解决问题....感谢反正 – BigBug 2011-12-28 04:03:34
如果您使用自己的ContentManager,您必须将RootDirectory属性设置为“内容“,或者如果你有不同的文件夹,那么你可以将其设置为。 – annonymously 2011-12-28 05:06:10