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