0

首先我想说这不是 Content.RootDirectory = "Content"; 问题。我已经尝试过使用和不使用那行代码。我有一个名为 FileFactory 的类,它有三个字典。一种用于纹理,一种用于声音,一种用于精灵字体。我还有其他工厂可以为其他类型的对象加载 XML 文件,并且它们工作得很好。这是加载字体的代码:

        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));
        }
    }

它给出的错误是:“加载“Content\fonts\ammoFont.xnb”时出错。找不到文件。” 即使该文件显然在那里,考虑到它必须在目录中才能作为字符串添加到 filePaths 数组中。我什至检查了文件夹,它就在那里。这是完整的错误:

Microsoft.Xna.Framework.Content.ContentLoadException 未处理
消息=加载“Content\fonts\ammoFont.xnb”时出错。文件未找到。
\Users\Chris\Documents\Visual Studio 2010\Projects\WindowsGame1\WindowsGame1\WindowsGame1\Game.cs:Microsoft.Xna.Framework.Game.RunGame(Boolean useBlockingRun) at Microsoft.Xna.Framework.Game.Run( ) 在 C:\Users\Chris\Documents\Visual Studio 2010\Projects\WindowsGame1\WindowsGame1\WindowsGame1\Program.cs:line 15 InnerException: System.IO.FileNotFoundException Message= 中的 WindowsGame1.Program.Main(String[] args)加载“Content\Content\fonts\ammoFont.xnb.xnb”时出错。文件未找到。Source=Microsoft.Xna.Framework StackTrace: 在 Microsoft.Xna.Framework.TitleContainer.OpenStream(String name) 在 Microsoft.Xna.Framework.Content.ContentManager.OpenStream(StringassetName) InnerException:

答:更改此代码:

Fonts.Add(key, content.Load<SpriteFont>(s));

对此:

Fonts.Add(key, content.Load<SpriteFont>("fonts\\" + key));
4

2 回答 2

0

游戏内容路径必须相对于您的Content.RootFolder(默认为“Content\”)并且不得包含文件扩展名(因此避免重复文件名,即使扩展名不同)。

于 2013-02-09T00:52:06.543 回答
0

从异常消息中,您调用它的字符串看起来是错误的。内容在那里两次,扩展名在那里两次:

Error loading "Content\Content\fonts\ammoFont.xnb.xnb"

您没有发布初始化数组的代码,但如果您在定义中有它们,请删除它们。

于 2013-02-09T02:23:20.000 回答