Zaki
Zaki

Reputation: 129

Displaying scores or game statistics on game complete

Please can anyone point me to any tutorial or give me like an outline of the steps to take that shows how to display game statistics like "scores" when the game ends in Cocos2d -iphone, I need some guidance here to implement a level complete scene for a game where on completion or at the end of the game, the level complete screen comes up and displays the scores, time used and the name of the player as seen in most games. I have carried out some research and so far I have seen examples where the game state has been saved using NSKeyedArchiver and NSKeyedUnarchiver as shown in the code below:

In the applicationWillTerminate method in appdelegate.mm

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];

[encoder encodeDouble:[[GameManager sharedGameManager]bestTime] forKey:@"bestTime"];
[encoder encodeInteger:[[GameManager sharedGameManager]livesLeft]  
           forKey:@"livesLeft"];
[encoder encodeInteger:[[GameManager sharedGameManager]currentLevel] 
            forKey:@"currentLevel"];

[encoder finishEncoding];
[gameData writeToFile:gameStatePath atomically:YES];
[encoder release];[/code]

and in my levelComplete.mm file I have

   NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,    
   NSUserDomainMask, YES);

   NSString *documentDirectory = [paths objectAtIndex:0];

   NSMutableData *gameData;
   NSKeyedUnarchiver *decoder;

   NSString *documentPath = [documentsDirectory    
   stringByAppendingPathComponent:@"gameState.dat"]; 
   gameData = [NSData dataWithContentsOfFile:documentPath];

   if(gameData) {

   decoder = [[NSKeyedUnarchiver alloc] initForReadingWithData:gameData];

   [[GameManager sharedGameManager] setCurrentLevel:[decoder   
            decodeIntegerForKey:@"currentLevel"]];
  [[GameManager sharedGameManager] setlivesLeft:[decoder   
                 decodeIntegerForKey:@"livesLeft"]];
[[GameManager sharedGameManager] setBestTime:[decoder   
              decodeDoubleForKey:@"bestTime"]];

[decoder release];

After this Information I seem to get stuck as to the next thing to do and I don't know if this is the right way to go. I will very much welcome any suggestions or outlines as to the path to take in implementing this.

Also my project has a singleton class, a gameplayLayer and a HUD layer where these variables are at present being displayed.

Thanks

Upvotes: 1

Views: 285

Answers (1)

msgambel
msgambel

Reputation: 7340

Here is a great little tutorial on how to make a simple space shooter app. I has a case at the end of the game which displays some information. There are also plenty of other tutorials on how to do basic game programming, with lots of sample code. Hope that Helps!

Upvotes: 1

Related Questions