簡體   English   中英

XNA Windows游戲(4.0)ContentLoadException

[英]XNA Windows Game (4.0) ContentLoadException

任何人都可以制作和編譯該程序( 單擊此處)直到第一個“快速構建”。 (直到他們說“這將是快速構建的好時機”)

由於某種原因,我不斷收到此異常!

我已經檢查了此Stackoverflow解決方案中提到的所有內容: 單擊此處,但沒有一個解決了我的問題:(

我的圖像放置在“內容”區域中。 它不在文件夾中。

請幫忙!

這是我的代碼:

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

如果您嚴格按照本教程進行操作,則將圖像添加到了游戲項目中,而不是內容項目中。 這是因為遵循了一個過時的教程(1.0與當前的4.0)

右鍵單擊Content Project,添加現有文件並添加圖像。

附帶說明一下,我強烈建議您使用File> New並在http://www.riemers.net/上進行教程有太多的代碼將更改從1.0更改為4.0,甚至嘗試制作1.0教程。

如果要使用自己的Content Manager目錄,則需要創建一個Content項目,然后在VS中將其根目錄設置為所需的屬性。 這會將已編譯資產的目標目錄從默認內容更改為您選擇的內容。 您也可以直接在默認的Content項目中設置此值。

然后,在實例化自己的實例時,指定“根目錄”並像往常一樣使用相對路徑加載資產。 它將在您選擇的根目錄中查找它們。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM