Added High Score Feature w/Data... now stuck
This might take a little time to explain, but please help...
I am using cocos2d, and OS 4.0.
I recently implemented a high-score feature - pretty basic (note none of these issues occur prior to implementing and works fine on OS 3.2 but the application has to load again, hence the point of multi-tasking and OS 4.0):
- When I open the game scene, the high score is loaded from a file.
- Then, once the player loses the high score is checked with the current score.
- If it's higher, write to the file then go to the game over scene which displays the current score and an up-to-date high score.
- There are some buttons in the game over scene - play again, other apps, main menu, etc. and when the user is working in the app moving back and forth from different layers and scenes, everything works fine.
When I exit the app I need to do so by holding down the icon in the bottom bar, then hitting the minus sign.
The issue at hand is that when I close the app from the bottom bar (I see SIGKILL in the bottom of xCode) and go to re-open the app, it just shows a frozen screen of where the app last was.
I then need to re-build/launch from xCode for the application to work properly again.
It seems like the solution is:
- Put in code to write to the file when the application is killed (when I see SIGKILL on xCode), when I'm pressing the minus sign as it's running in the background (similar to applicationWillTerminate in the pre 4.0 days when the home-button is pressed)
Any idea how I detect this "SIGKILL"?
Here is some of the code I'm using to write to the array file:
dataFilePath method:
- (NSString *)dataFilePath
{
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDire ctory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
return [documentsDirectory stringByAppendingPathComponent:kFilename];
}
Pulling info from the file code:
NSString *filePath = [self dataFilePath];
if ([[NSFileManager defaultManager] fileExistsAtPath:filePath])
{
NSArray *array = [[NSArray alloc] initWithContentsOfFile:filePath];
globalTopScoreString = [array objectAtIndex:0];
globalTopScore = [globalTopScoreString integerValue];
[array release];
}
else
{
globalTopScore = 0;
}
Writing to the file:
NSMutableArray *array = [[NSMutableArray alloc] init];
globalTopScoreString = [NSString stringWithFormat:@"%i", globalTopScore];
[array addObject:globalTopScoreString];
[array writeToFile:[self dataFilePath] atomically:YES];
[array release];
Last edited by ama269; 06-19-2010 at 09:09 PM.
|