【问题标题】:Xna Windows Phone 7 Saving Game DataXna Windows Phone 7 保存游戏数据
【发布时间】:2014-04-12 18:53:26
【问题描述】:

我正在尝试找出如何将高分数据保存到 Windows Phone 上的隔离存储中。我搜索了很多东西,但没有发现任何工作。有谁知道我该怎么做?

【问题讨论】:

    标签: xna


    【解决方案1】:

    以下答案取自此 MSDN 条目:

    http://msdn.microsoft.com/en-us/library/ff604992.aspx

    XNA Game Studio 4.0 Refresh 不提供对可写的访问 Windows Phone 上的存储。要访问此类存储,您需要使用 System.IO.IsolatedStorage 命名空间中的类。

    对于 Windows Phone 项目,Visual Studio 会自动添加 包含 System.IO.IsolatedStorage 的程序集到您的项目中。那里 无需为您的项目添加任何额外的引用。

    将数据写入独立存储的示例:

    protected override void OnExiting(object sender, System.EventArgs args)
            {
                // Save the game state (in this case, the high score).
                IsolatedStorageFile savegameStorage = IsolatedStorageFile.GetUserStoreForApplication();
    
                // open isolated storage, and write the savefile.
                IsolatedStorageFileStream fs = null;
                using (fs = savegameStorage.CreateFile(SAVEFILENAME))
                {
                    if (fs != null)
                    {
                        // just overwrite the existing info for this example.
                        byte[] bytes = System.BitConverter.GetBytes(highScore);
                        fs.Write(bytes, 0, bytes.Length);
                    }
                }
    
                base.OnExiting(sender, args);
            }
    

    【讨论】:

    • 谢谢,我如何在 xna @Nahuell 中找到退出事件。
    • 我现在不能在我的机器上尝试它,因为我的 WP 模拟器有一些问题,但是 onExiting 方法是游戏类的一部分,你应该没有问题将它添加到你的 Game1 类:@ 987654322@如果不行,也可以在UnloadContent方法中添加代码,退出游戏前执行。
    • @user3453481 更新,我能够在一个 Windows 游戏项目上做到这一点,我可以保证你可以将方法添加到 Game1 类。只需键入受保护的覆盖(空格),所有可以被覆盖的方法都会显示给您。 protected override void OnExiting(object sender, EventArgs args) { base.OnExiting(sender, args); }
    • 它并没有真正起作用。它无法打开文件。文件名必须是什么(格式)。 @Nahuell。
    • 如果我没记错的话,格式应该是 . (example.txt, example.xml)。你遇到了什么错误?您是否保存在其他目录中? @user3453481
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-12-31
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多