XNA文件夹层次结构

时间:2011-06-11 00:05:24

标签: file xna

好的,所以我是XNA的新手,我只是想让一个图像显示在屏幕上。我相信我已经将图像添加到VS2010程序中的内容文件夹中,但是当我尝试运行程序时,我收到一条错误,指出找不到文件。所以我想知道什么文件夹有图像才能调用图像文件Tank.png。 代码很简单:

namespace Aceldama_Windows_Game
{
/// <summary>
/// This is the main type for your game
/// </summary>
public class Game1 : Microsoft.Xna.Framework.Game
{
    GraphicsDeviceManager graphics;
    SpriteBatch 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);
    }
}

}

我应该将图像放入哪个文件夹才能直接调用文件而无需放入完整的文件路径?基本上问题在于,似乎无论我在哪里放置tank.png文件(无论是在带有可执行文件/ c#文件的文件中还是在内容文件夹中)

2 个答案:

答案 0 :(得分:0)

XNA内容管道获取内容项目引用的项目并将其转换为XNB文件。 ContentManager然后加载那些XNB文件。

因此,首先要检查的是,是否正在创建XNB文件,而不是在输出目录中创建它们。

根据您发布的代码Content.RootDirectory = "Content"Content.Load<Texture2D>("Tank"),假设Windows Debug版本,它将查找文件:

 bin/x86/Debug/Content/Tank.xnb

如果您更改了内容项目输出目录,则还需要更改您在代码中设置的RootDirectory

答案 1 :(得分:0)

所以我在无休止地搜索网页后弄清楚问题是什么; p代码很好问题是我需要在项目中创建对内容文件夹的引用。上传了解决方案资源管理器的图片,以显示我需要做的事情! enter image description here

在“内容参考”下,我没有添加“Aceldama_windows_gameContent”。需要在那里引用游戏内容文件夹及其中的图像!