如何在cocos2d中编写Plist文件以将数据从一个级别传输到另一个级别

时间:2013-04-01 05:56:26

标签: cocos2d-iphone plist box2d-iphone

-(void)writeToTopScorePlist<br>
{
   <br>
   NSError *error;<br>
   NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); //1<br>
   NSString *documentsDirectory = [paths objectAtIndex:0]; //2<br>
   NSString *path = [documentsDirectory stringByAppendingPathComponent:@"TopScore.plist"]; //3<br>
   NSFileManager *fileManager = [NSFileManager defaultManager];<br>
   if (![fileManager fileExistsAtPath: path]) //4<br>
   {<br>
          NSString *bundle = [[NSBundle mainBundle] pathForResource:@"TopScore" ofType:@"plist"]; //5<br>
          [fileManager copyItemAtPath:bundle toPath: path error:&error]; //6<br>
   }<br>
   NSMutableDictionary *data = [[NSMutableDictionary alloc] initWithContentsOfFile:path ];<br>
    [data setObject:[NSString stringWithFormat:@"%i",self.MyTopScore] forKey:@"TopScore"];<br>
    [data writeToFile:path atomically:YES];<br>
    [data release] ;<br>
   }

1 个答案:

答案 0 :(得分:0)

使用NSUserDefaults:

// Code used to save your highscore in the preference
int topscore  = yourGameScore;
[[NSUserDefaults standardUserDefaults] setObject:[NSNumber numberWithInt:topscore] forKey:@"topscore"];
[[NSUserDefaults standardUserDefaults] synchronize];


// Code used to get your saved value from the prefs.
topScore = [[[NSUserDefaults standardUserDefaults] objectForKey:@"topscore"] intValue ];