【发布时间】: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,也存在同样的问题....
-
我们需要知道异常中的消息,而不仅仅是类型。