我只是想知道,難以保存/加載遊戲狀態?什麼是我保存並加載遊戲狀態的最佳方式
我正在創建一款遊戲,並且我希望能夠保存遊戲狀態以防萬一出現通話或玩家結束遊戲繼續玩另一次遊戲或遊戲崩潰時,則可選擇開始新遊戲比賽或繼續他/她離開的地方。
遊戲狀態包含SFX,背景音樂(背景音樂應該繼續它結束的地方)。 (兩個按鈕),一個充滿uiimages的網格(圖像應該重新出現在遊戲結束之前的相同位置),得分(高分和玩家核心),等級和3個以外的網格。
你有什麼建議?
有沒有好的教程呢?我已經檢查過,但我似乎只能找到cocos2d,而我目前沒有使用cocos2d。
//Load the saved gamestate
- (void)loadGameState {
// Set up the file manager and documents path
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0]; NSMutableData *gameData;
NSKeyedUnarchiver *decoder;
NSString *documentPath = [documentsDirectory stringByAppendingPathComponent:@"gameState.dat"];
gameData = [NSData dataWithContentsOfFile:documentPath];
decoder = [[NSKeyedUnarchiver alloc] initForReadingWithData:gameData];
NSLog(@"INFO - GameScene: Loading saved game duration.");
//timeSinceGameStarted = [[decoder decodeObjectForKey:@"timeSinceGameStarted"] floatValue];
NSLog(@"INGO - GameScene: Loading saved game score.");
playerscore = [[decoder decodeObjectForKey:@"playerscore"] floatValue];
NSLog(@"INGO - GameScene: Loading saved high score.");
highscore = [[decoder decodeObjectForKey:@"highscore"] floatValue];
NSLog(@"INGO - GameScene: Loading saved level.");
level = [[decoder decodeObjectForKey:@"Level"] floatValue];
NSLog(@"INFO - GameScene: Loading game time data.");
[decoder release];
}
//Saving the current gamestate
-(void)saveGameState {
NSLog(@"Saving game state");
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *gameStatePath = [documentsDirectory stringByAppendingPathComponent:@"gameState.dat"];
NSMutableData *gameData;
NSKeyedArchiver *encoder;
gameData = [NSMutableData data];
encoder = [[NSKeyedArchiver alloc] initForWritingWithMutableData:gameData];
// Archive the games timer settings
NSNumber *savedGameStartTime = [NSNumber numberWithFloat:totalSeconds];
//NSNumber *savedTimeSinceGameStarted = [NSNumber numberWithFloat:timeSinceGameStarted];
NSNumber *savedPlayerScore = [NSNumber numberWithFloat:playerscore];
NSNumber *savedHighScore = [NSNumber numberWithFloat:highscore];
NSNumber *savedcurrentlevel = [NSNumber numberWithFloat:level];
[encoder encodeObject:savedGameStartTime forKey:@"gameStartTime"];
//[encoder encodeObject:savedTimeSinceGameStarted forKey:@"timeSinceGameStarted"];
[encoder encodeObject:savedPlayerScore forKey:@"playerscore"];
[encoder encodeObject:savedHighScore forKey:@"highscore"];
[encoder encodeObject:savedcurrentlevel forKey:@"Level"];
// Finish encoding and write the contents of gameData to file
[encoder finishEncoding];
[gameData writeToFile:gameStatePath atomically:YES];
[encoder release];
// Tell the game controller that a resumed game is available
//sharedGameController.resumedGameAvailable = YES;
}
你可以保存gamestate在Sqlite數據庫 – 2011-05-26 07:46:40
好嗎?我該怎麼做?這如果我的第一場比賽,所以我還是相當新的這個東西 – iphonedevonthemake 2011-05-26 07:47:59