首先,我想指出,這不是一個Content.RootDirectory =「內容」;問題。我已經試過這個,沒有這行代碼。我有一個名爲FileFactory的類,它有三個字典。一個用於紋理,一個用於聲音,另一個用於精靈字體。我也有其他的工廠爲其他類型的對象加載XML文件,這些工作完美。下面是加載字體代碼:XNA ContentLoadException當文件是明顯存在
public static void LoadFonts(ContentManager content)
{
string[] filePaths = Directory.GetFiles(@"Content\fonts\");
string key;
foreach (string s in filePaths)
{
key = Path.GetFileNameWithoutExtension(s);
Fonts.Add(key, content.Load<SpriteFont>(s));
}
}
它給人的錯誤是:「錯誤加載‘內容\ Fonts \中ammoFont.xnb’找不到文件。」即使該文件是CLEARLY,也要考慮將其作爲字符串添加到filePaths數組中的目錄。我甚至檢查過這個文件夾,它就在那裏。以下是完整的錯誤:
Microsoft.Xna.Framework.Content.ContentLoadException was unhandled
Message=Error loading "Content\fonts\ammoFont.xnb". File not found.
Source=Microsoft.Xna.Framework StackTrace: at Microsoft.Xna.Framework.Content.ContentManager.OpenStream(String assetName) at Microsoft.Xna.Framework.Content.ContentManager.ReadAsset[T](String assetName, Action`1 recordDisposableObject) at Microsoft.Xna.Framework.Content.ContentManager.Load[T](String assetName) at OfUnknownOrigin.FileFactory.LoadFonts(ContentManager content) in C:\Users\Chris\Documents\Visual Studio 2010\Projects\WindowsGame1\WindowsGame1\WindowsGame1\FileFactory.cs:line 38 at WindowsGame1.UnknownOrigin.LoadContent() in C:\Users\Chris\Documents\Visual Studio 2010\Projects\WindowsGame1\WindowsGame1\WindowsGame1\Game.cs:line 110 at Microsoft.Xna.Framework.Game.Initialize() at WindowsGame1.UnknownOrigin.Initialize() in C:\Users\Chris\Documents\Visual Studio 2010\Projects\WindowsGame1\WindowsGame1\WindowsGame1\Game.cs:line 102 at Microsoft.Xna.Framework.Game.RunGame(Boolean useBlockingRun) at Microsoft.Xna.Framework.Game.Run() at WindowsGame1.Program.Main(String[] args) in C:\Users\Chris\Documents\Visual Studio 2010\Projects\WindowsGame1\WindowsGame1\WindowsGame1\Program.cs:line 15 InnerException: System.IO.FileNotFoundException Message=Error loading "Content\Content\fonts\ammoFont.xnb.xnb". File not found. Source=Microsoft.Xna.Framework StackTrace: at Microsoft.Xna.Framework.TitleContainer.OpenStream(String name) at Microsoft.Xna.Framework.Content.ContentManager.OpenStream(String assetName) InnerException:
答: 更改此代碼:
Fonts.Add(key, content.Load<SpriteFont>(s));
這樣:
Fonts.Add(key, content.Load<SpriteFont>("fonts\\" + key));
作爲測試,確實'Content.Load( 「字體/ ammoFont」);'自身工作中的'LoadFonts()'方法之外? –
2013-02-09 16:26:34
是的,這確實有效。這就是我在創建FileFactory類之前所做的。 – Bagofsheep 2013-02-09 19:13:33