2012-04-23 111 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