Thanks again...it seems like for every question that's answered a bunch more pop up
The Level Select Screen isn't my main screen. And I noticed that the data saving code I posted earlier saves upon applicationWillTerminate but the user may not be on the Level Select Screen when terminating the app so I can't have it there.
So I need to be able to load and save data whenever Level Select Screen loads. Which I can do in viewDidLoad. That way if the user has beat a level, the Level Select Screen automatically appears, and the levelModeArray is updated in the plist.
Is there a way to implement all the data saving methods in viewDidLoad? Here's what I think could be done :/ I took a wild guess at changing the notification portion
Code:
LevelSelectView.m
-(NSString *)pathOfFile
{
NSArray *path = NSSearchPathForDirectoriesInDomain(NSDocumentDirectory, NSUserDomainMask, YES);
// I'm getting this warning:<implicit declaration of function 'NSSearchPathForDirectoriesInDomain'>
NSString *documentFolder = [path objectAtIndex:0];
return [documentFolder stringByAppendingFormat:@"myfile.plist"];
}
//Notice that the applicationWillTerminate code is gone and moved to viewDidLoad
-(void) viewDidLoad:(NSNotification*)notification //This is probably wrong
{
NSString *filePath = [self pathOfFile];
if ([[NSFileManager defaultManager]fileExistsAtPath:filePath])
{
NSArray *array = [[NSArray alloc]initWithContentsOfFile:filePath];
if(difficultyFlag == 0)
{
NSMutableArray *tempLevelArray = [NSMutableArray arrayWithArray:[array objectAtIndex:0]];
//objectAtIndex 0 since EasyModeArray was stored here previously
}
else if(difficultyFlag == 1)
{
NSMutableArray *tempLevelArray = [NSMutableArray arrayWithArray:[array objectAtIndex:1]];
//objectAtIndex 1 since HardModeArray was stored here previously
}
else if(difficultyFlag == 2)
{
NSMutableArray *tempLevelArray = [NSMutableArray arrayWithArray:[array objectAtIndex:2]];
//objectAtIndex 2 since ExpertModeArray was stored here previously
}
[array release];
// This would check whether Level Screen View was loaded by the Game Screen and it passed a WIN
// in that case I need to update tempLevelArray accordingly where LevelNumber is the level that was beat
if(winFlag == 1)
{
NSNumber *levelBeat = 1;
[tempLevelArray replaceObjectAtIndex:LevelNumber withObject:levelBeat]; // Is this the correct way to replace an object in tempLevelArray?
}
//This is taken from the deleted applicationWillTerminate method
NSMutableArray *array = [[NSMutableArray alloc] init];
[array replaceObjectAtIndex:difficultyFlag withObject:tempLevelArray];
[array writeToFile:[self pathOfFile] atomically:YES];
[array release];
//Here I'd update the level images accordingly
//
//
}
else //This is the first time opening the app, I initialize each mode array and store it in plist
{
NSMutableArray *EasyMode = [[NSMutableArray alloc] initWithCapacity:48];
for(int i = 0;i < 49;i++)
{
[EasyMode addObject:[NSNumber numberWithInt:0]];
}
NSMutableArray *HardMode = [[NSMutableArray alloc] initWithCapacity:48];
for(int i = 0;i < 49;i++)
{
[HardMode addObject:[NSNumber numberWithInt:0]];
}
NSMutableArray *ExpertMode = [[NSMutableArray alloc] initWithCapacity:48];
for(int i = 0;i < 49;i++)
{
[ExpertMode addObject:[NSNumber numberWithInt:0]];
}
NSMutableArray *array = [[NSMutableArray alloc] initWithCapacity:3];
[array addObject:EasyMode];
[array addObject:HardMode];
[array addObject:ExpertMode];
[array writeToFile:[self pathOfFile] atomically:YES];
[array release];
}
//This is my attempt at changing the Notification portion for viewDidLoad implementation, I doubt it's correct and I'm not sure where it's supposed to go
UIApplication *app = [UIApplication sharedApplication];
[[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(viewDidLoad:) name:UIApplicationViewDidLoadNotification object:app];
}
Just so you get a clear sense of how the critical parts of the app work, imagine that the user has already beat EasyLevels{1,2,3,4,5}, HardLevels{1,2,3}, and none of the ExpertLevels.
The app opens to a menu screen with two buttons. The first button goes to the Level Select screen, and the second button changes the difficultyFlag.
If the difficulty is set to Easy and the user hits the first button they see 48 squares. 1,2,3,4 and 5 are black (because they've been beat) and the rest are white.
If they return to the menu and change the difficulty to Hard and then hit the first button on the menu screen, they see 48 squares. 1,2 and 3 are black and the rest are white.
Same goes for expert difficulty.
Pressing one of these squares loads the actual "game board" with that level's presets.
What I'm trying to solve here is loading the correct grid of black or white squares depending on the difficulty. Hope that makes this clearer.