2012-04-18 9 views
3

私のゲームでは、レベルが完了すると、アプリはドキュメントディレクトリのファイルに「1」を保存します。ゲームがロードされると、プレイヤーは前のレベルが完了している場合にのみレベルをプレイすることができます。私がXcodeを介してゲームをテストし、デバイス上でアプリケーションが正常に動作し、レベルは前のレベルが完了するまで再生できません。しかし、App StoreでAppが承認されてリリースされると、Appは各レベルが完了したように動作します(ロックされたレベルはありません)。私はこれを理解することができず、誰かの助けに感謝します!私がテストしているデバイスはすべてiOS 5.0以上です。以下はドキュメントディレクトリからのファイルの保存と読み取りiOS 5

Documentsディレクトリにある完成度を保存したコードである:以下

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 

を、それが明示的にあなたのコードで「真」に変更する必要がありますので、あなたは「偽」にそれを初期化します。

+0

良い点は、それ以上にあるかもしれないようです... int isLevelCompletedが作成されると、その中にガベージが存在する可能性がありますが、そのチャンスは常に "1"と同じです。私はあなたの助言に従い、ブール値に変更するつもりです – user1233894

関連する問題