2012-04-18 136 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