Asset not found XNA

烈酒焚心 提交于 2019-12-17 10:05:47

问题


I am very new to XNA and I began by following a tutorial that draws an image on the screen. I was able to move my image into the Content folder but when I try to use it in my code, it can't be found.

I am using the asset name and I just cannot find what I am doing wrong. The tutorials use XNA 3.0 and I am using Visual Studio 2010, not sure if that matters or not.

Here is my code

public class Game1 : Microsoft.Xna.Framework.Game
{
    Vector2 mPosition = new Vector2(0, 0);
    Texture2D mSpriteTexture;

    GraphicsDeviceManager graphics;
    SpriteBatch spriteBatch;

    public Game1()
    {
        graphics = new GraphicsDeviceManager(this);
        Content.RootDirectory = "Content";
    }


    protected override void Initialize()
    {
        base.Initialize();
    }

    protected override void LoadContent()
    {
        spriteBatch = new SpriteBatch(GraphicsDevice);

        mSpriteTexture = Content.Load<Texture2D>("Face");
    }


    protected override void UnloadContent()
    {
    }

    protected override void Update(GameTime gameTime)
    {

        if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed)
            this.Exit();


        base.Update(gameTime);
    }

    protected override void Draw(GameTime gameTime)
    {
        GraphicsDevice.Clear(Color.Black);

        spriteBatch.Begin();
        spriteBatch.Draw(mSpriteTexture, mPosition, Color.White);
        spriteBatch.End();

        base.Draw(gameTime);
    }
}

}

The error reads "ContentLoadException was Unhandled. File not found.

Solution Explorer

I hope this is enough information. Also the asset name of my file is Face.

Thanks in advance.


回答1:


If you've added your file to the Content project (those are new for 4.0) the other things to check would be to make sure the file is one of the supported formats for a Texture2D (.jpg, .png, .bmp, .tga). After that, click on the image and verify that the asset name is correct and matching the exact casing/spelling that you're using in code to load it by that name. If that's correct then also make sure that the Content Importer for the image is set correctly to be a Texture2D. And then another thing to verify would be to make sure you're image is in the root of the Content project and not in a folder. If you have it in a folder, then you need to include the folder name (or names) when loading it.

If you've verified all that then you may need to post an image or a sample project so that we can take a look and see if we spot anything that way.

From that screenshot it looks like you need to right-click on the "Test" project and say "Add Content Reference". You'll then need to pick your "Test (Content)" project as that reference. That should have happened by default when you created this new game project and I'm not sure why it looks like it was removed.




回答2:


As you are using Visual Studio 2010, I'm guessing that you are using XNA 4.0. If this is the case, there is a new Content Reference project where you put all of your textures, sounds, models etc. into. If you have created a Content folder in the XNA Game project, this won't work.




回答3:


In XNA 4.0 Content Reference project has a Content Root Directory property (set to Content by default) that specifies the name of the subdirectory that will hold the final output files of pipeline content generated from the project folder. Therefore, if you create Content directory inside Content Reference project the Face asset will be placed in Content/Content directory and you will have to load it like this

mSpriteTexture = Content.Load<Texture2D>(@"Content/Face");

despite setting Content.RootDirectory = "Content"




回答4:


Another potential solution:

Check your original file (e.g. MyTexture.bmp) and make sure the Build Action is set to Compile.

If you have it set to something else, such as the logical-seeming Content, it won't work.



来源:https://stackoverflow.com/questions/4644325/asset-not-found-xna

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!