2012-11-26 26 views
2

我正在嘗試爲我的遊戲創建一個模塊,並且我的想法是能夠使用我的所有遊戲。DrawableGameComponent作爲庫,如何處理內容?

所以我的lib項目有它自己的內容文件夾和它的一切都好,但它與主機項目的內容文件夾崩潰。

這裏是我的組件(這是它自己的項目,我REF鏈接到主體項目(手機應用程序)):

namespace FPSComponent 
{ 
    public class FPS : DrawableGameComponent 
    { 
     private SpriteBatch spriteBatch; 

     SpriteFont normal; 
     Texture2D headerBackground; 

     public FPS(Game game) 
      : base(game) 
     { 
      spriteBatch = new SpriteBatch(Game.GraphicsDevice); 

      string tempRootDirectory = Game.Content.RootDirectory; 
      Game.Content.RootDirectory = "FPSComponentContent"; 

      headerBackground = Game.Content.Load<Texture2D>("header-background"); 

      normal = Game.Content.Load<SpriteFont>("normal"); 

      //Game.Content.RootDirectory = tempRootDirectory; <-- does not work 
     } 

     public override void Update(GameTime gameTime) 
     { 
      // TODO: Add your update logic here 

      base.Update(gameTime); 
     } 

     public override void Draw(GameTime gameTime) 
     { 
      //GraphicsDevice.Clear(Color.CornflowerBlue); 

      spriteBatch.Begin(); 

      spriteBatch.Draw(headerBackground, Vector2.Zero, Color.White); 

      spriteBatch.DrawString(normal, "FPS COMPONENT", new Vector2(20, 20), Color.White); 

      spriteBatch.End(); 

      base.Draw(gameTime); 
     } 
    } 
} 

什麼即時試圖做的哪裏是創建一個獨立的組件來導入到我的項目,這主要是爲ATM學習的目的......而不是一個工作FPS例如:-)

+0

我已經設法通過將內容文件夾作爲庫的一部分來解決這個問題,我已經爲其複製了預編譯資源。這種方法的不足之處在於,您必須將資源從「內容」文件夾複製到構建位置(引用該庫時)。如果你想我會發布它,但我希望看到一個更好的方式來做到這一點... – neeKo

回答

1

我想你一定看過這篇文章Content Pipeline MSDN

這裏是一點,當你按下F5在Visual Studio中它會編譯你的項目和你的資產。

通過使用Game.Content.Load<Texture2D> XNA會嘗試找出一個.xnb

當Visual Studio是編譯項目,他正在創建.xnb到特定forlder。

你是不是真的加載.png文件或直接.jpg文件到您的遊戲:)

您必須先編譯你的新資產注入.xnb文件(一種序列化過程)

這裏是一個類似的問題https://gamedev.stackexchange.com/