Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
146 views
Welcome To Ask or Share your Answers For Others

1 Answer

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...