2012-01-27 39 views
0

我正在編寫一個cocos2d遊戲,在大多數遊戲中,您必須先完成前一級以繼續進入下一級。每個級別都在不同的層(節點)。我想將整數(1-20)存儲到屬性列表中,以便在整個遊戲中保留變量。是的,我一直在搜索互聯網約一個小時,找不到任何東西。將變量存儲在屬性列表文件中Cocos2d

我發現了一個代碼來存儲變量,但我不知道如何正確使用它。

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

看一看此http://計算器。 com/questions/8732405/is-there-a-video-for-writing-to-plist-in-ios – Sbhklr 2012-01-27 23:45:26

+0

thx ill看看 – mattblessed 2012-01-27 23:47:34

+0

我的代碼一直在返回(null) – mattblessed 2012-01-28 02:18:10

回答

0

下面是一些示例代碼,以得到一個想法(使用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 
+0

當我將變量保存在一個圖層中並嘗試並在閱讀它記錄使用NSLog的NSLog(@「值:%@」,值);'它返回'(Value:null)' – mattblessed 2012-01-28 00:24:22

+0

代碼中有一個錯字,當然字典必須像這樣初始化:NSMutableDictionary * plistDict = [[NSMutableDictionary alloc] init]; 嘗試編輯的示例代碼。現在它應該工作。 – Sbhklr 2012-01-28 09:16:05

+0

謝謝你現在的作品! – mattblessed 2012-01-28 22:01:52

相關問題