如何在游戏中存储用户数据? (迅速,spritekit) [英] How to store user data in game? (swift, spritekit)

查看:152
本文介绍了如何在游戏中存储用户数据? (迅速,spritekit)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我有一个游戏,一开始用户会选择一些名称,性别等东西,这会影响游戏的许多方面,例如用于渲染用户的精灵,

Let's say I had a game where in the beginning the user gets to choose a couple of things like name, gender, etc, and that will affect a lot of aspects in the game such as the sprite used to render the user, and the text that is displayed.

因此,每次我离开游戏的一个区域并输入一个新区域时,我都应该根据用户数据弄清楚-精灵是什么使用以及准备什么文字。我是否应该使用仅用于用户数据的字典来创建单独的类,然后在每次进入新区域时都调用它?
谢谢。

So every time I leave one area of the game and enter a new one I should figure out according to the user data - what sprite to use and what text to prep. Should I make a separate class with a dictionary just for the user's data and then just call it whenever I enter a new area? Thanks yall.

推荐答案

有几种方法可以做到这一点。

There is a few ways to do this.

我更喜欢使用带有NSCoding的Singleton GameData类来容纳所有需要永久保存的属性(数组,字典等)。

I prefer to use a singleton GameData class with NSCoding to house all the properties (arrays, dictionaries etc) that need to be saved permanently.

我之所以喜欢它,是因为它使代码更具可读性,并且具有可以从项目中任何位置访问属性的优点。这样,还很容易包含icloud键值存储。

I like it this way because it makes code more readable and also has the advantage that you can access the properties from anywhere in your project. This way it's also easy to include icloud key value storage.

您可以阅读我回答的这两个问题,以简单地了解其外观

You can read these 2 questions I answered for a simple example of how this can look

SpriteKit:为什么要等一轮分数才能更新? (迅速)

将自定义类保存到NSUserDefaults中,是否有比使用NSCoder编码和解码所有内容更好的方法?

这里也有一些不错的教程

There is a also few good tutorials around such as this

https://www.hackingwithswift.com/read/12/3/fixing-project-10-nscoding

要安全保存数据,您可以/应该使用钥匙串。要了解更多信息,请查看此问题。

To securely save data you can/should use keychain. To read more about it check out this question.

NSUserDefaults在iOS 8,9上的安全性如何?

这篇关于如何在游戏中存储用户数据? (迅速,spritekit)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆