2011-06-11 63 views
1

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

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#文件的文件中还是位于内容文件夹中),似乎都好像无论在哪里。

+0

尝试将其更改为:'mSpriteTexture = this.Content.Load (“Tank”);' – aligray 2011-06-11 00:08:54

+0

我也是这么做的,也是同样的问题。错误是找不到文件 – Zieklecknerizer 2011-06-11 00:36:32

回答

0

所以我找出了无尽的搜索网络后出现的问题; p代码很好,问题是我需要创建项目中的内容文件夹的引用。上传了解决方案资源管理器的照片,以显示我需要做的事情! enter image description here

根据内容引用我没有添加“Aceldama_windows_gameContent”。这需要在那里引用游戏内容文件夹中的图像!

0

XNA内容管道将条目由您的内容项目引用并将它们转换为XNB文件。然后ContentManager加载这些XNB文件。

因此,首先要检查的是XNB文件是否在您希望它们位于输出目录中的位置创建。

按照您发布的代码,用Content.RootDirectory = "Content"Content.Load<Texture2D>("Tank"),假设一个Windows调试版本,将需要的文件:

bin/x86/Debug/Content/Tank.xnb 

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

+0

所以我检查了它们是否被创建为.xnb文件,但它们不是。将.xnb文件导入项目本身时,它们是否会创建? – Zieklecknerizer 2011-06-11 02:02:19

+1

它们是作为构建过程的一部分而创建的。请阅读[此MSDN项目](http://msdn.microsoft.com/zh-cn/library/bb313966.aspx)以获取有关如何添加内容以使其正确构建的说明。和[整个部分](http://msdn.microsoft.com/en-us/library/bb203887.aspx)以获得完整的概述。 – 2011-06-11 03:08:41

相关问题