开发者

XNA Folder Hierarchy

Ok so I'm new to XNA and I am just trying to get an image to show on the screen. I believe I have added the Images to the content folder in the VS2010 program HOWEVER when I try to run the program I get an error saying File not found. So I am wondering what folder to have the image in to just be able to call the image file Tank.png. the code is simple:

namespace Aceldama_Windows_Game
{
/// <summary>
/// This is the main type for your game
/// </summary>
public class Game1 : Microsoft.Xna.Framework.Game
{
    GraphicsDeviceManager graphics;
    SpriteBatc开发者_如何学JAVAh spriteBatch;
    Vector2 mPosition = new Vector2(0, 0);
    Texture2D mSpriteTexture;
    public Game1()
    {
        graphics = new GraphicsDeviceManager(this);
        Content.RootDirectory = "Content";

    }

    /// <summary>
    /// Allows the game to perform any initialization it needs to before starting to run.
    /// This is where it can query for any required services and load any non-graphic
    /// related content.  Calling base.Initialize will enumerate through any components
    /// and initialize them as well.
    /// </summary>
    protected override void Initialize()
    {
        // TODO: Add your initialization logic here

        base.Initialize();
    }

    /// <summary>
    /// LoadContent will be called once per game and is the place to load
    /// all of your content.
    /// </summary>
    protected override void LoadContent()
    {
        // Create a new SpriteBatch, which can be used to draw textures.
        spriteBatch = new SpriteBatch(GraphicsDevice);

        // TODO: use this.Content to load your game content here
        mSpriteTexture = this.Content.Load<Texture2D>("Tank");
    }

    /// <summary>
    /// UnloadContent will be called once per game and is the place to unload
    /// all content.
    /// </summary>
    protected override void UnloadContent()
    {
        // TODO: Unload any non ContentManager content here
    }

    /// <summary>
    /// Allows the game to run logic such as updating the world,
    /// checking for collisions, gathering input, and playing audio.
    /// </summary>
    /// <param name="gameTime">Provides a snapshot of timing values.</param>
    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);
    }

    /// <summary>
    /// This is called when the game should draw itself.
    /// </summary>
    /// <param name="gameTime">Provides a snapshot of timing values.</param>
    protected override void Draw(GameTime gameTime)
    {
        GraphicsDevice.Clear(Color.CornflowerBlue);

        // TODO: Add your drawing code here
        spriteBatch.Begin();
        spriteBatch.Draw(mSpriteTexture, mPosition, Color.White);
        spriteBatch.End();

        base.Draw(gameTime);
    }
}

}

What folder should I put the image into to be able to call the file directly without having to put the full file path? Essentially the problem being is it seems as though no matter where i place the tank.png file (whether it is in the the file with the executable/c# files or in the content folder)


The XNA Content Pipeline takes the items that are referenced by your content project and transforms them into XNB files. The ContentManager then loads those XNB files.

So the first thing to check is if the XNB files are being created where you expect them in your output directory.

According to the code you have posted, with Content.RootDirectory = "Content" and Content.Load<Texture2D>("Tank"), assuming a Windows Debug build, it will be looking for the file:

 bin/x86/Debug/Content/Tank.xnb

If you changed the content project output directory, you need to change the RootDirectory you set in code as well.


So I figured out what the problem was after searching the web endlessly;p it The code was fine the problem was I needed to create a reference to the content folder in the project. uploaded a pic of the solution explorer to show what I needed to do!

XNA Folder Hierarchy

Under Content References I did not have the "Aceldama_windows_gameContent" Added. That needs to be there to reference the game content folder with the images in it!

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜