对我来说保存和保存的最佳方式是什么?加载游戏状态
我只是想知道,保存/加载游戏状态很难吗?
我正在创建一个游戏,我希望能够保存游戏状态,以防有电话或玩家结束游戏以继续玩其他时间或游戏崩溃,然后选择开始新游戏或继续他/她离开了。
游戏状态包含 SFX、背景音乐(背景音乐应在结束处继续)。 (两者的按钮)、一个充满 uiimages 的网格(图像应重新出现在游戏结束前的同一位置)、得分(高分和玩家分数)、关卡以及网格外的另外 3 个 uiimages。
你有什么建议吗?
那里有什么好的教程吗?我已经检查过,但似乎只找到了 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;
}
I was just wondering, is it hard to save / load gamestate?
I'm creating a game and I want to be able to save the gamestate in case there's a call or the player ends the game to continue to play another time or the game crashes, then give the choice to start a new game or continue where he/she left off.
The gamestate contains SFX, background music (the background music should continue where it ended). (buttons to both), a grid full of uiimages (the images should reappear at the same spot they where before the game ended), score (both highscore and playerscore), level and 3 more uiimages outside the grid.
What do you recommend?
Are there any good tutorials out there? I've checked but I only seem to find for cocos2d and I'm not using cocos2d at the moment.
//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;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我想到的一种方法是将所有这些信息存储在 plist 或类似的文件中;事物的坐标和位置、音乐曲目的时间等,然后当向用户提供恢复上一次会话的选项时,您只需读取所述 plist 的内容并将所有内容放回到原来的位置即可。
我知道这是一个特别粗略的答案,但这种方法已经在 2 & 中发挥了作用。我过去开发过的 3D 应用程序 - 诚然,我们只跟踪了几十个项目 - 我确信您的游戏可能会更大。
回应您的评论:
为了实现此目的,我使用了 NSUserDefaults - 因此您可以通过查看 Apple 网站上的类参考来找到一些有用的信息。在相关位置创建一个 NSUserDefaults 成员,然后可以轮询图形实例和音乐类等以了解它们的进度 - 因此您需要编写一个方法来为要保存的每个类返回此信息 - 或:对于每个帧,让相应的类自动将其最新位置等写入默认文件。要设置默认文件...
为了使其正常工作,您需要创建一个名为 Defaults 或任何您喜欢的文件的 plist 文件。然后使用类引用中的方法调用,您可以轻松地向其中写入字符串和整数等。
The one method that comes to my mind would be to store all of this information in a plist or similar; the coordinates and positions of things, the time of the music track etc, and then when the user is presented with the option to resume their last session, you can just read the contents of said plist and put everything back where it was.
I understand this is a particularly crude answer, but this methodology has worked in the 2 & 3D apps I have worked on in the past - admittedly we were only tracking a few dozen items - I am sure your game is likely to be much larger.
In response to your comment:
To accomplish this, I used NSUserDefaults - so you will find some good information by looking at the class reference on the Apple site. Create a NSUserDefaults member in a relevant place which can then poll your graphics' instances and music classes etc for how far along they are - so you'll need to write a method that returns this information for each class that you want to save - or: for each frame, get the respective classes to automatically write their latest positions etc to your defaults file. To setup the defaults file...
In order for this to work, you need to create a plist file called Defaults or whatever you like. Then using the method calls located in the class reference, you can easily write strings and ints etc to it.