在Cocos2d属性列表文件中存储变量
我正在编写一个 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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
下面是一些示例代码来了解一下(使用 ARC 进行内存管理):
输出:
Here is some sample code to get an idea (uses ARC for memory management):
Output: