2012-04-23 85 views
3

我不确定我应该如何保存我正在开发的游戏的游戏状态。我应该保存一个包含所有游戏信息的实例/对象吗?如果是,如何?还是应该将所有相关信息保存在.txt文件中,并在需要时保存/加载信息?储蓄游戏状态Android

您是如何做到这一点的?您如何看待我的建议?

回答

3

除非你序列,并将其保存到一些文本/二进制文件/数据库文件无法保存的实例/对象。因此你的两种选择是完全相同的。

您需要保存的是您需要重建游戏状态的所有信息。可能有些信息可以从这里获得。

如果你只有一小组固定的变量来定义你的游戏状态,那么使用SharedPreferences。

如果你想保留多个状态和/或更复杂的保存使用一些文本(XML,JSON,...)/二进制/数据库/ ..表示和存储。

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(); 
+0

关于解析,你可以检索一个ParseObject而不知道它的ID? – 2012-07-10 22:15:49

0

您可以使用SQLite数据库来保存游戏中的重要变量。如果游戏是从一个类开始的,你可以为这个类提供两个构造函数,一个从头开始实例化一个正常游戏,另一个接受游戏中的所有变量,并从保存点创建游戏对象。

这将允许您有多个游戏保存(通过保存id和任何数据),如果您更新游戏(假设您不更改数据库),游戏保存不会丢失。

快速搜索“构造函数重载”以了解更多信息。

0

如果你的游戏是不是数据密集型的,如果序列化和保存到共享偏好是不够的,你可以看看我写的,以方便存储和检索活动的必填字段图书馆的GNStateManager组件标志着我@GNState注释。它使用起来很简单。其他单例类对象状态也可以保存。看到这里设置和使用信息:https://github.com/noxiouswinter/gnlib_android/wiki/gnstatemanager