2012-04-18 135 views
3

在我的遊戲中,當完成關卡時,應用程序在應用程序的Documents目錄中的文件中存儲「1」。當遊戲加載時,如果先前的等級已經完成,玩家只能玩一個等級。當我通過Xcode測試遊戲並在設備上測試應用程序時,應用程序可以正常運行,並且在上一個關卡完成之前無法播放關卡。但是,當應用程序在App Store上獲得批准併發布時,該應用程序的行爲就像每個級別都已完成(無鎖定級別)。我無法弄清楚這一點,並會感謝某人的幫助!我正在測試的設備都是iOs 5.0或更高版本。從文檔目錄中存儲和讀取文件iOS 5

下面是保存在文件目錄中完成級別代碼:

NSMutableData* data = [[NSMutableData alloc] init]; 
     NSKeyedArchiver* coder = [[NSKeyedArchiver alloc] initForWritingWithMutableData:data]; 
     NSString *levelString = [NSString stringWithFormat:@"Level%d",level]; 
     [coder encodeInteger:1 forKey:levelString]; 
     [coder finishEncoding]; 
     NSString *levelString2 = [NSString stringWithFormat:@"Level%d.save",level]; 
     /// 
     NSFileManager *filemgr; 
     NSString *dataFile; 
     NSString *docsDir; 
     NSArray *dirPaths; 

     filemgr = [NSFileManager defaultManager]; 

     // Identify the documents directory 
     dirPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); 

     docsDir = [dirPaths objectAtIndex:0]; 

     // Build the path to the data file 
     dataFile = [docsDir stringByAppendingPathComponent:levelString2]; 

     // Check if the file already exists 
     if ([filemgr fileExistsAtPath: dataFile]) 
     { 
      [[NSFileManager defaultManager] removeItemAtPath:dataFile error:nil]; 
     } 
     [data writeToFile:dataFile atomically:YES]; 
     [coder release]; 
     [data release]; 
    } 
    @catch (NSException* ex) 
    { 
     CCLOG(@"level save failed: %@", ex); 
    } 

下面是讀取文件目錄,看看水平已經完成了代碼:

if ([self loadCompletedLevels:6] == 1) { //// level gets unlocked **** } 

-(int) loadCompletedLevels:(int)theLevel; { 

int isLevelCompleted; //1 = completed 

NSString* kSaveFile = [NSString stringWithFormat:@"Level%d.save",theLevel]; 
NSString *levelString = [NSString stringWithFormat:@"Level%d",theLevel]; 

@try 
{ 

    NSFileManager *filemgr; 
    NSString *dataFile; 
    NSString *docsDir; 
    NSArray *dirPaths; 

    filemgr = [NSFileManager defaultManager]; 

    // Identify the documents directory 
    dirPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); 

    docsDir = [dirPaths objectAtIndex:0]; 

    // Build the path to the data file 
    dataFile = [docsDir stringByAppendingPathComponent:kSaveFile]; 


    if ([[NSFileManager defaultManager] fileExistsAtPath:dataFile]) 
    { 
     NSData* data = [[NSData alloc] initWithContentsOfFile:dataFile]; 

     if (data && [data length] > 0) 
     { 
      NSKeyedUnarchiver* decoder = [[NSKeyedUnarchiver alloc] initForReadingWithData:data]; 
      isLevelCompleted = [decoder decodeIntForKey:levelString]; 

      [decoder release]; 
     } 

     [data release]; 
    } 
    if (isLevelCompleted == 1) { 
     levelCompleted = YES; 

    } 
} 
@catch (NSException* ex) 
{ 

    levelCompleted = NO; 
} 
return isLevelCompleted; } 

回答

0

您應該使用不同的方法來存儲數據,但真正的問題是您沒有初始化返回值isLevelCompleted。它在堆棧中,並沒有默認值。它從堆棧位置發生的任何事情開始。

所以,如果你沒有設置它,它將有一個任意值。

此外,你應該使用BOOL一個布爾值,但如果你讓這樣的:

int isLevelCompleted = 0; //1 = completed 

您將初始化爲「假」,因此必須顯式更改爲「true」你的代碼。

+0

好點,好像可以有更多的東西,雖然...當int isLevelCompleted創建時,它可能有垃圾,但它總是等於「1」的機會是什麼。我會遵循你的建議,並改變爲一個布爾值 – user1233894 2012-04-18 16:00:36