Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7178481
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T16:55:47+00:00 2026-05-28T16:55:47+00:00

I am programming a cocos2d game where, in most games, you must complete the

  • 0

I am programming a cocos2d game where, in most games, you must complete the level previous to proceed on to the next level. Each level is in a different layer (node). I wanted to store integers (1-20) to the property list so the variables are retained throughout the entire game. And yes i have been searching the internet for about an hour and cant find anything.

I found a code to store variables but I don’t know how to use it correctly.

NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); 
    NSString *documentPath = [paths objectAtIndex:0];
    NSString *path = [documentPath stringByAppendingPathComponent:@"levelscompleted.save"];


    NSMutableDictionary* myDict = [[NSMutableDictionary alloc] init];


    myDict = [NSKeyedUnarchiver unarchiveObjectWithFile:path];
    NSString *nssLevelsCompleted = [myDict objectForKey:@"LevelsCompleted"];
    LevelsCompleted = [nssLevelsCompleted intValue];

    LevelsCompleted = 445;


    [myDict setObject:nssLevelsCompleted forKey:@"LevelsCompleted"];



    [NSKeyedArchiver archiveRootObject:myDict toFile:path];
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-28T16:55:48+00:00Added an answer on May 28, 2026 at 4:55 pm

    Here is some sample code to get an idea (uses ARC for memory management):

    - (NSString*) filePath {
        NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
        NSString *documentsDirectory = [paths objectAtIndex:0];
        NSString *filePath = [NSString stringWithFormat:@"%@/%@",documentsDirectory,@"myfile.plist"];
        return filePath;
    }
    
    - (void)writeScoreToPlist:(NSInteger)score level:(NSString*)level {
        NSMutableDictionary* plistDict = [[NSMutableDictionary alloc] initWithContentsOfFile:[self filePath]];    
        if(plistDict==nil){
            plistDict = [[NSMutableDictionary alloc] init];    
        }
        [plistDict setObject:[NSNumber numberWithInteger:score] forKey:level];
        [plistDict writeToFile:[self filePath] atomically: YES];
    }
    
    - (NSInteger)readScoreFromPlist:(NSString*)level {
        NSMutableDictionary* plistDict = [[NSMutableDictionary alloc] initWithContentsOfFile:[self filePath]];
        NSNumber *value = [plistDict objectForKey:level];
        return value.integerValue;
    }
    
    - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
    {
        self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
        // Override point for customization after application launch.
    
        [self writeScoreToPlist:999 level:@"Level1Highscore"];
        NSInteger score = [self readScoreFromPlist:@"Level1Highscore"];
        NSLog(@"Achieved score for level 1: %d",score);
    
        self.window.backgroundColor = [UIColor whiteColor];
        [self.window makeKeyAndVisible];
        return YES;
    }
    

    Output:

    2012-01-29 16:24:30.844 plistTest[23531:f803] Achieved score for level 1: 999
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to study Cocos2d, seems it's a good framework for iPhone game programming.
I recently started game programming on the iPhone using Cocos2d and Box2d. So here's
I'm programming a basic game in cocos2d, and whenever I transition into one of
Any recommendations on good sites/resources on programming with the Cocos2d-iPhone game engine? What worked
im new to programming, i have been experimenting with cocos2d, heres the problem, i
(programming in iPhone objective-C) I have a class level NSString* that I create, add
I'm new to game programming. And i have a question. I want to have
Im programming a little game where two Players have to click some Buttons it
I'm fairly new to game programming (but not to programming) and I want to
I'm relatively new to iphone programming, and I've been starting out with cocos2d. I

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.