2011-06-11 68 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

相關問題