在 iOS 上保存用户游戏分数
我正在开发一款 iOS 游戏,我有两个问题。首先,我将关卡信息(分数、背景图像等)维护到 plist 文件中。第一次启动应用程序时,我将 plist 文件从资源目录复制到文档目录(我需要将用户最好成绩写入其中)。退出维护这些信息的最佳方式是什么?其次,如果我对应用程序进行更新,例如添加新级别,如何在不丢失当前用户分数的情况下添加更新新信息?
感谢您的阅读。
I am developing an iOS game and I have two questions. First, I am maintaining the levels information (scores, background image, etc) into a plist file. The first time that the app is launched, I copy the plist file from the resources directory to the documents directory (I need to write the user best scores into it). Exits a best way to maintaing this information? Second, if I make an update of the app, adding new levels par example, how can I add the update new information without losing the current user scores?
Thanks for reading.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
属性列表似乎是跟踪高分等的好方法。如果您已经做到了这一点,请坚持下去,直到您有充分的理由进行更改。
当用户更新到应用程序的新版本时,您的数据文件(实际上是 Documents 目录的全部内容)将被保留。
A property list seems as good a way to keep track of high scores and such as any other. If you've already got that working, stick with it until you have a good reason to change.
Your data file (indeed, the entire contents of the Documents directory) will be preserved when the user updates to a new version of the app.
正如 Caleb 所说,pList 文件可能适合保存高分。
我想补充一些关于#2 的内容。为了简单起见,只要您提前计划更新,您就可以始终重复使用相同的保存文件。这比尝试为不同版本处理不同的 plist 文件要容易得多。
当您创建游戏保存 plist 文件时,请确保包含保存游戏格式版本号字段。这样,对于未来的版本,您可以打开保存文件,读取版本号,并进行任何必要的调整以将其升级到当前版本。当玩家基本上可以跳过更新、下载版本 1 和 3 但跳过 2 时,这非常方便。
AS Caleb said, a pList file is probably fine for saving high scores.
I'd like to add a bit about #2. You can just always re-use the same save file for simplicity, as long as you plan ahead for updates. This is a lot easier than trying to juggle different plist files for different versions.
When you create your game save plist file, just be sure to include a field for a save game format version number. That way, for future releases, you can open the save file, read the version number, and make any necessary adjustments to bring it up to the current version. This is extremely handy when players can essentially skip updates, downloading version 1 and 3 but skipping 2.
我同意迦勒的观点。最好有一些源代码来帮助您。
我创建了此示例代码,它具有本地和全球支持。
I agree with Caleb. It's best to have a little source code to help you.
I created this sample code that has both local and global support.
您可能想看看 NSUserDefaults 类,提供存储应用数据的基本管理功能。对于更高级的数据存储,CoreData 是推荐的方式。
You might want to have a look at the NSUserDefaults class which provides basic management capabilities for storing app data. For more advanced data storage, CoreData is the recommended way to go.