2012-04-23 21 views
3

私が開発しているゲームのゲーム状態をどのように保存するのかは分かりません。すべてのゲーム情報を含むインスタンス/オブジェクトを保存する必要がありますか?はいの場合、どうですか?または、すべての関連情報を.txtファイルに保存し、必要に応じて情報を保存/読み込む必要がありますか?ゲームの状態を保存するAndroid

これはどうやって行い、私の提案についてどう思いますか?

答えて

3

あなたはそれをシリアル化しない限り、インスタンス/オブジェクトを保存し、いくつかのテキスト/バイナリ/データベースファイルに保存することはできません。あなたの2つの選択肢は一種のものです。

保存する必要があるのは、ゲームの状態を再構築するために必要なすべての情報です。おそらくここから派生できる情報があります。

あなたのgamestateを定義する小さな固定セットの変数がある場合は、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

Parseについては、Idを知らずにParseObjectを取得できますか? –

0

SQLiteデータベースを使用して、ゲームの重要な変数を保存することができます。ゲームが1つのクラスから開始された場合、そのクラスに2つのコンストラクタを提供することができます。最初から通常のゲームをインスタンス化するものと、ゲームのすべての変数を受け入れてセーブポイントからゲームオブジェクトを作成するものです。

これにより、複数のゲームを保存することができます(IDを任意のデータと共に保存することができます)。ゲームを更新するとゲームセーブは失われません。

詳細については、「コンストラクタのオーバーロード」を参照してください。

0

あなたのゲームがデータ集約型ではなく、シリアライズして共有設定に保存すれば充分です。私が書いたライブラリのGNStateManagerコンポーネントをチェックアウトして、myとマークされたアクティビティの必須フィールド@GNState注釈。使い方が簡単ではありません。他のシングルトンクラスのオブジェクト状態も保存できます。 https://github.com/noxiouswinter/gnlib_android/wiki/gnstatemanager

関連する問題