3

私のゲームでは、レベルが完了すると、アプリはアプリのDocumentsディレクトリにあるファイルに「1」を保存します。その後、ゲームが読み込まれると、プレーヤーは前のレベルが完了している場合にのみレベルをプレイできます。Xcodeを介してデバイスでゲームをテストすると、アプリは正しく動作し、前のレベルが完了するまでレベルを再生できません。ただし、アプリが承認されてApp Storeでリリースされると、アプリは各レベルが完了したかのように動作します(ロックされたレベルはありません)。私はこれを理解することができません、そして誰かの助けをいただければ幸いです!私がテストしているデバイスはすべてiO5.0以降です。

以下は、完了したレベルを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);
    }

以下は、レベルが完了したかどうかを確認するためにDocumentディレクトリを読み取るコードです。

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; }
4

1 に答える 1

0

データを格納するためにおそらく別のアプローチを使用する必要がありますが、実際の問題は、戻り値isLevelCompletedを初期化していないことです。スタック上にあり、デフォルト値はありません。それは、そのスタック位置にたまたまあるものから始まります。

したがって、設定しないと任意の値になります。

また、ブール値にはおそらくBOOLを使用する必要がありますが、これを行う場合は次のようになります。

int isLevelCompleted = 0;  //1 = completed

これを「false」に初期化するため、コードで明示的に「true」に変更する必要があります。

于 2012-04-18T03:56:24.573 に答える