在Cocos2d属性列表文件中存储变量

发布于 2024-12-29 13:28:55 字数 872 浏览 5 评论 0原文

我正在编写一个 cocos2d 游戏,在大多数游戏中,您必须完成上一个级别才能进入下一个级别。每个级别都位于不同的层(节点)中。我想将整数 (1-20) 存储到属性列表中,以便在整个游戏中保留变量。是的,我已经在互联网上搜索了大约一个小时,但找不到任何东西。

我找到了一个存储变量的代码,但我不知道如何正确使用它。

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

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

若无相欠,怎会相见 2025-01-05 13:28:55

下面是一些示例代码来了解一下(使用 ARC 进行内存管理):

- (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;
}

输出:

2012-01-29 16:24:30.844 plistTest[23531:f803] Achieved score for level 1: 999

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
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文