私は iPhone 用の RPG ゲームを作成していますが、すべてうまくいっていますが、ユーザーがバックグラウンドで実行されているアプリを閉じてもゲーム全体が最初からやり直さないように、ゲーム レベルを保存する方法を知る必要があります。 . 古いスタイルのゲームを復活させて、中断したところからパスワードを入力する必要があるようにすることも考えていました. しかし、それでもゲームを適切に保存する方法がわかりません。さらに、ゲームを保存したとしても、アプリを完全に閉じても保存したままにするにはどうすればよいですか? これまでのところ、セーブデータコードをAppWillTerminate
行に追加しようとしましたが、まだ何もしていません. どんな助けでも大歓迎です。
2 に答える
1
ユーザーがどのレベルにいたかを保存したいのか、それともゲームの状態を保存したいのかわかりません。ユーザーがどのレベルにいたかを単純に保存したい場合は、@EricS のメソッド (NSUserDefaults) を使用する必要があります。ゲームの状態を保存するのはもう少し複雑です。私はこのようなことをします:
//Writing game state to file
//Some sample data
int lives = player.kLives;
int enemiesKilled = player.kEnemiesKilled;
int ammo = player.currentAmmo;
//Storing the sample data in an array
NSArray *gameState = [[NSArray alloc] initWithObjects: [NSNumber numberWithInt:lives], [NSNumber numberWithInt:enemiesKilled], [NSNumber numberWithInt:ammo], nil];
//Writing the array to a .plist file located at "path"
if([gameState writeToFile:path atomically:YES]) {
NSLog(@"Success!");
}
//Reading from file
//Reads the array stored in a .plist located at "path"
NSArray *lastGameState = [NSArray arrayWithContentsOfFile:path];
.plist は次のようになります。
配列を使用すると、ゲームの状態をリロードするときに、アイテムを保存した順序を知る必要がありますが、これはそれほど悪くはありませんが、より信頼性の高い方法が必要な場合は、このような NSDictionary を使用してみてください。 :
//Writing game state to file
//Some sample data
int lives = player.kLives;
int enemiesKilled = player.kEnemiesKilled;
int ammo = player.currentAmmo;
int points = player.currentPoints;
//Store the sample data objects in an array
NSArray *gameStateObjects = [NSArray arrayWithObjects:[NSNumber numberWithInt:lives], [NSNumber numberWithInt:enemiesKilled], [NSNumber numberWithInt:points], [NSNumber numberWithInt:ammo], nil];
//Store their keys in a separate array
NSArray *gameStateKeys = [NSArray arrayWithObjects:@"lives", @"enemiesKilled", @"points", @"ammo", nil];
//Storing the objects and keys in a dictionary
NSDictionary *gameStateDict = [NSDictionary dictionaryWithObjects:gameStateObjects forKeys:gameStateKeys];
//Write to file
[gameStateDict writeToFile:path atomically: YES];
//Reading from file
//Reads the array stored in a .plist located at "path"
NSDictionary *lastGameState = [NSDictionary dictionaryWithContentsOfFile:path];
辞書 .plist は次のようになります。
于 2012-08-23T06:07:23.710 に答える
0
レベルを保存するには:
[[NSUserDefaults standardUserDefaults] setInteger:5 forKey:@"level"];
レベルを読むには:
NSInteger level = [[NSUserDefaults standardUserDefaults] integerForKey:@"level"];
ユーザーがそのレベルに入るたびに設定します。バックグラウンドに送られるまで待つこともできますが、待っていても意味がありません。
于 2012-08-23T03:42:19.160 に答える