Search code examples
objective-ccocos2d-iphonespritebuilder

How can I save results of every game for a long time and compare new result with previous?


I am making a game using SpriteBuilder + cocos2d-iphone v3. I made a global variable that stores a score during the game. But how can I save results of every time for a long time and compare new result with previous ?


Solution

  • I recommend Core Data. This will allow you to save a record for each game, which you can later query, sort, and present in useful ways.

    Another approach is to use NSUserDefaults. You can store an array like this:

    // Set scores
    NSArray *scores = @[@97, @76, @88];
    [[NSUserDefaults standardUserDefaults] setObject:scores forKey:@"gameScores"];
    // Get scores
    scores = [[NSUserDefaults standardUserDefaults] objectForKey:@"gameScores"];
    

    This might be a reasonable approach to take if you only need to store limited data and don't want to use a database.