【问题标题】:XNA XBOX highscore portXNA XBOX 高分端口
【发布时间】:2011-10-20 17:25:12
【问题描述】:

我正在尝试将我的 pc XNA 游戏移植到 xbox,并尝试在我现有的 pc 文件管理的同时实现 xna easystorage 以获得高分。基本上试图将http://xnaessentials.com/tutorials/highscores.aspx/tutorials/highscores.aspxhttp://easystorage.codeplex.com/结合起来

我遇到了一个关于 LoadHighScores() 的特定错误,即“return (data);”错误- 使用未分配的局部变量“数据”。

我认为这是由于 easystorage/xbox 的异步设计!?但不确定如何解决 - 以下是代码示例:

原始 PC 代码:(适用于 PC)

public static HighScoreData LoadHighScores(字符串文件名) { HighScoreData 数据; // 获取游戏保存路径

string fullpath = "Content/highscores.lst";
// Open the file
FileStream stream = File.Open(fullpath, FileMode.Open,FileAccess.Read);    
        try    
        {         // Read the data from the file
    XmlSerializer serializer = new XmlSerializer(typeof(HighScoreData));
    data = (HighScoreData)serializer.Deserialize(stream);    
        }    
        finally
         {        // Close the file        
            stream.Close();    
        }     
        return (data);
    }

XBOX 端口:(有错误)

public static HighScoreData LoadHighScores(字符串容器,字符串文件名) { HighScoreData 数据;

        if (Global.SaveDevice.FileExists(container, filename))
                {
                    Global.SaveDevice.Load(container, filename, stream =>
                            {
                                File.Open(Global.fileName_options, FileMode.Open,//FileMode.OpenOrCreate,
                                         FileAccess.Read);  
                                try    
                            {         
                                            // Read the data from the file
                                        XmlSerializer serializer = new XmlSerializer(typeof(HighScoreData));
                                        data = (HighScoreData)serializer.Deserialize(stream);    
                            }    
                              finally
                             {      
                                    // Close the file  

                                stream.Close();

                             }   

                            });

                }


        return (data);
    }

有什么想法吗?

【问题讨论】:

    标签: xna xbox


    【解决方案1】:

    返回前分配数据。 ;)

      data = (if_struct) ? new your_struct() : null;
      if (Global.SaveDevice.FileExists(container, filename))
      {
         ......
      }
      return (data);
    }
    

    【讨论】:

    • +1 我注意到 Xbox 在这方面可能会非常挑剔,而桌面 CLR 则更加宽容......
    • 谢谢,但这不起作用,因为数据是一个结构并且它们是不可为空的 - 还有其他想法吗?公共结构 HighScoreData { 公共字符串 [] 名称;公共 int[] 分数;公共 int[] 级别;公共整数计数; public HighScoreData(int count) { Name = new string[count];分数 = 新的 int[count];级别 = 新的 int[count];计数=计数; } }
    • 在这种情况下,您只需输入 data = new MyStruct()。编译器抱怨有可能在没有为数据赋值的情况下到达方法的出口..
    • 谢谢 - 我想我将不得不重新考虑整个方法,或者在我的大脑清醒之前休息一下!就像我原始帖子第一个链接中概述的 PC 版本一样 - 我' initailise 的前十名并创建文件,当移植尝试在选择 savedevice 后移动它时,如果不存在文件,则通过执行 createcores(),但 global.savedevice 仍然未定义并且没有创建文件,甚至虽然我得到提示选择内存选项。在 xbox 上保存数据比 PC 复杂得多!必须是一种将一堆数字保存到文件的更简单方法:)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-03-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多