Saving with Cocos2d

This tutorial will show you how to save an integer to your iphone.

Start by downloading the SaveCount Code.

The first bit of code that is very important is in the SaveCountAppDelegate.m file.

+ (void)initialize{
        NSDictionary *appDefaults = [NSDictionary
                                                                 dictionaryWithObjects:[NSArray arrayWithObjects:
                                                                                                                [NSNumber numberWithInt:0],
                                                                                                                nil]
                                                                 forKeys:[NSArray arrayWithObjects:
                                                                                  @"Count",
                                                                                  nil]];
    [[NSUserDefaults standardUserDefaults]
         registerDefaults:appDefaults];
}
 

This code is run when your app is first installed on an iphone or ipod touch, the code creates an NSNumber called count that is initialized to 0.

In MainMenu.m, If you run the code you will see a menu with a new game, load game, help, and exit buttons. When you click the new game button the following code will run.

[[NSUserDefaults standardUserDefaults] setObject:[NSNumber  numberWithInt:0] forKey:@"Count"];

        SaveCountAppDelegate *delegate = [[UIApplication sharedApplication] delegate];
        [delegate nextLevel];
 

The first line sets the Count that we initialized back to 0, so that any time you click on the new game button Count starts over.
Then we call the delegate and run its’ function nextLevel.

Now the last line of interest is in the HelloWorldScene.m file, in the reset function.

count=[[NSUserDefaults standardUserDefaults]
                        integerForKey:@"Count"];
 

This line sets the count integer, that is defined in the HelloWorldScene.h file, to what ever integer is in Count, and that is all there is to saving.

If you have any questions feel free to comment.

Copy right Britney Lee Johnson October 2010.

This entry was posted in cocos2d. Bookmark the permalink.

4 Responses to Saving with Cocos2d

  1. Hassan says:

    I see a lot of interesting posts on your website.
    You have to spend a lot of time writing, i know how to save you a lot of
    time, there is a tool that creates unique, google friendly articles in couple
    of seconds, just search in google – laranita’s free content source

  2. AndreaWarrior says:

    Hi, i don’t know how it work with my game.
    I create, in my GamePlayScene a label: *_scoreLabel and a Integer: *_points for update my label.
    I want to create a _highScoreLabel in my MainScene but i don’t know how can i do it?!?
    Can you explain me where i must to add my -(void)? Thank you :)

  3. thanks, This excellent website certainly has all the information

Leave a Reply to AndreaWarrior Cancel reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>