私のゲームでは、レベルが完了すると、アプリはアプリの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; }