我不确定我应该如何保存我正在开发的游戏状态。我应该保存包含所有游戏信息的实例/对象吗?如果有,怎么样?或者我应该将所有相关信息保存在.txt文件中并在需要时保存/加载信息吗?
你是如何做到这一点的,你如何看待我的建议?
答案 0 :(得分:3)
除非您将其序列化并将其保存到某个文本/二进制/数据库文件,否则无法保存实例/对象。因此,你的两个选择是相同的。
您需要保存的是重建游戏状态所需的所有信息。您可以从这里获得一些信息。
如果您只有一小组固定的变量来定义您的游戏状态,那么请使用SharedPreferences。
如果你想保留多个状态和/或保存更复杂,可以使用一些文本(xml,json,...)/ binary / database / ..表示并存储它。
答案 1 :(得分:1)
我可以建议使用Parse。
https://parse.com/docs/android_guide#objects
The ParseObject
Storing data on Parse is built around the ParseObject. Each ParseObject contains key-value pairs of JSON-compatible data. This data is schemaless, which means that you don't need to specify ahead of time what keys exist on each ParseObject. You simply set whatever key-value pairs you want, and our backend will store it.
For example, let's say you're tracking high scores for a game. A single ParseObject could contain:
score: 1337, playerName: "Sean Plott", cheatMode: false
Keys must be alphanumeric strings. Values can be strings, numbers, booleans, or even arrays and objects - anything that can be JSON-encoded.
Each ParseObject has a class name that you can use to distinguish different sorts of data. For example, we could call the high score object a GameScore. We recommend that you NameYourClassesLikeThis and nameYourKeysLikeThis, just to keep your code looking pretty.
Saving Objects
Let's say you want to save the GameScore described above to the server. The interface is similar to a Map, plus the save method:
ParseObject gameScore = new ParseObject("GameScore");
gameScore.put("score", 1337);
gameScore.put("playerName", "Sean Plott");
gameScore.put("cheatMode", false);
try {
gameScore.save();
} catch (ParseException e) {
// e.getMessage() will have information on the error.
}
After this code runs, you will probably be wondering if anything really happened. To make sure the data was saved, you can look at the Data Browser in your app on Parse. You should see something like this:
objectId: "xWMyZ4YEGZ", score: 1337, playerName: "Sean Plott", cheatMode: false,
createdAt:"2011-06-10T18:33:42Z", updatedAt:"2011-06-10T18:33:42Z"
There are two things to note here. You didn't have to configure or set up a new Class called GameScore before running this code. Your Parse app lazily creates this Class for you when it first encounters it.
There are also a few fields you don't need to specify that are provided as a convenience. objectId is a unique identifier for each saved object. createdAt and updatedAt represent the time that each object was created and last modified on the server. Each of these fields is filled in by the server, so they don't exist on a ParseObject until a save operation has completed.
Retrieving Objects
Saving data to the cloud is fun, but it's even more fun to get that data out again. If you have the objectId, you can retrieve the whole ParseObject using a ParseQuery:
ParseQuery query = new ParseQuery("GameScore");
ParseObject gameScore;
try {
gameScore = query.get("xWMyZ4YEGZ");
} catch (ParseException e) {
// e.getMessage() will have information on the error.
}
To get the values out of the ParseObject, there's a getX method for each data type:
int score = gameScore.getInt("score");
String playerName = gameScore.getString("playerName");
boolean cheatMode = gameScore.getBoolean("cheatMode");
If you don't know what type of data you're getting out, you can call get(key), but then you probably have to cast it right away anyways. In most situations you should use the typed accessors like getString.
The three special values have their own accessors:
String objectId = gameScore.getObjectId();
Date updatedAt = gameScore.getUpdatedAt();
Date createdAt = gameScore.getCreatedAt();
If you need to refresh an object you already have with the latest data that is on the server, you can call the refresh method like so:
myObject.refresh();
答案 2 :(得分:0)
您可以使用SQLite数据库来保存游戏中的重要变量。如果游戏是从一个类开始的,那么你可以为这个类提供两个构造函数,一个从头开始实例化一个普通游戏,另一个从游戏中接受所有变量并从保存点创建游戏对象。
这将允许您有多个游戏保存(通过保存ID和任何数据),如果您更新游戏(假设您不改变数据库),游戏保存不会丢失。
快速搜索"构造函数重载"了解更多信息。
答案 3 :(得分:0)
如果您的游戏不是数据密集型,如果序列化并保存到共享首选项就足够了,您可以查看我编写的库的GNStateManager组件,以便于存储和检索标记为my @的活动的必填字段。 GNState注释。它很简单易用。其他单例类对象状态也可以保存。请参阅此处了解设置和使用信息:https://github.com/noxiouswinter/gnlib_android/wiki/gnstatemanager