applicationWillTerminate / ApplicationDidEnterBackground问题

时间:2011-07-13 14:27:52

标签: iphone xcode cocoa-touch

我需要你的帮助来解决这个问题。

我找到了一个关于如何保存/加载游戏状态Save Game State Tutorial的教程,我觉得我自己做得很好但是我被卡住了。

首先,这是我在将游戏移动到后台并退出游戏时需要保存的内容。

* Playerscore

*排行榜

*等级

*时间

*网格和它上面的砖块(网格是6x6,我有35块可以在网格内移动的砖块)//不知道如何保存/加载这部分

*红宝石的数量

*设置状态,如果音乐按钮以及SFX按钮打开或关闭//不知道如何保存/加载此部分

*音乐的状态,是在歌曲或中间等的开头,当tehg ame停止//不知道如何保存/加载这部分

我不知道如何在暂停状态下加载游戏,并在用户离开之前显示Ready,Set,Go图像。 (不知道如何将它们连接到我的UIApplicationDelegate中的applicationDidEnterBackground和applicationWillTerminate,与保存部分相同)

然后我还想知道主菜单是否存在已保存的游戏状态,让用户在新游戏之间做出决定并继续。

下面的代码显示了我在mainmenu.m文件和gameview.m文件中的操作方式。如果你想看到游戏控制器,请告诉我......

mainmenu.h     //游戏控制器     GameController * sharedGameController;

mainmenu.m

//The game begins either by continue or by resetting the gamestate
-(IBAction)start{

    NSLog(@"Start");

    newgame.hidden = NO;

    if(sharedGameController.resumedGameAvailable == YES){
        //IF there is a gamestate saved then activate the continue button
        continuegame.hidden = YES;
        continuegame.alpha = 1.0;
        continuegame.enabled = YES;
    }
    else{

        continuegame.hidden = NO;
        continuegame.alpha = 0.5;
        continuegame.enabled = NO;

    }
    korridor1.hidden = NO;
    fackla.hidden = NO;
    Start.hidden = YES;
    option.hidden = YES;
    howtoplay.hidden = YES;
    SFXimage.hidden = YES; 
    Musicimage.hidden = YES; 
    back.hidden = NO;

}

//Continue previous game
-(IBAction)ContinueGame{

    [audio stop];
    audio.numberOfLoops = 0;
    [audio release];
    NSLog(@"Continue");

    back.hidden = YES;
    newgame.hidden = YES;
    continuegame.hidden = YES;
    korridor1.hidden = NO;
    fackla.hidden = YES;
    Start.hidden = YES;
    option.hidden = YES;
    howtoplay.hidden = YES;
    SFXimage.hidden = YES; 
    Musicimage.hidden = YES;  
    [UIView beginAnimations:@"korridor_1.png" context:nil];
    [UIView setAnimationDuration:1];
    korridor1.transform = CGAffineTransformMakeScale(13,13);
    korridor1.alpha = 1.0f;
    [UIView commitAnimations];


    //The Game begins
    [NSTimer scheduledTimerWithTimeInterval:1.5 target:self selector:@selector(GameView) userInfo:nil repeats:NO];
}

gameview.m

//Load the saved gamestate
- (void)loadGameState {

    // Set up the file manager and documents path
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentsDirectory = [paths objectAtIndex:0]; NSMutableData *gameData;
    NSKeyedUnarchiver *decoder;
    NSString *documentPath = [documentsDirectory stringByAppendingPathComponent:@"gameState.dat"];
    gameData = [NSData dataWithContentsOfFile:documentPath]; 
    decoder = [[NSKeyedUnarchiver alloc] initForReadingWithData:gameData];
    NSLog(@"INFO - GameScene: Loading saved game duration."); 
    //timeSinceGameStarted = [[decoder decodeObjectForKey:@"timeSinceGameStarted"] floatValue];
    NSLog(@"INGO - GameScene: Loading saved game score."); 
    playerscore = [[decoder decodeObjectForKey:@"playerscore"] floatValue];
    NSLog(@"INGO - GameScene: Loading saved high score."); 
    highscore = [[decoder decodeObjectForKey:@"highscore"] floatValue];
    NSLog(@"INGO - GameScene: Loading saved level."); 
    level = [[decoder decodeObjectForKey:@"Level"] floatValue];
    NSLog(@"INGO - GameScene: Loading saved amount of rubies."); 
    totalrubies = [[decoder decodeObjectForKey:@"totalrubies"] floatValue];
    NSLog(@"INFO - GameScene: Loading game time data."); 
    [decoder release];
}

//Saving the current gamestate
-(void)saveGameState {

    NSLog(@"Saving game state");

    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentsDirectory = [paths objectAtIndex:0]; 
    NSString *gameStatePath = [documentsDirectory stringByAppendingPathComponent:@"gameState.dat"];

    NSMutableData *gameData; 
    NSKeyedArchiver *encoder; 
    gameData = [NSMutableData data]; 
    encoder = [[NSKeyedArchiver alloc] initForWritingWithMutableData:gameData];

    // Archive the games timer settings
    NSNumber *savedGameStartTime = [NSNumber numberWithFloat:totalSeconds]; 
    //NSNumber *savedTimeSinceGameStarted = [NSNumber numberWithFloat:timeSinceGameStarted]; 
    // Archive the players score
    NSNumber *savedPlayerScore = [NSNumber numberWithFloat:playerscore];
    // Archive the games high score
    NSNumber *savedHighScore = [NSNumber numberWithFloat:highscore];
    // Archive the current game level
    NSNumber *savedcurrentlevel = [NSNumber numberWithFloat:level];
    // Archive the total amount of rubies
    NSNumber *savedTotalRubies = [NSNumber numberWithFloat:totalrubies];

    [encoder encodeObject:savedGameStartTime forKey:@"gameStartTime"];
    //[encoder encodeObject:savedTimeSinceGameStarted forKey:@"timeSinceGameStarted"];

    [encoder encodeObject:savedPlayerScore forKey:@"playerscore"]; 
    [encoder encodeObject:savedHighScore forKey:@"highscore"]; 

    [encoder encodeObject:savedcurrentlevel forKey:@"Level"]; 

    [encoder encodeObject:savedTotalRubies forKey:@"totalrubies"];

    // Finish encoding and write the contents of gameData to file
    [encoder finishEncoding]; 
    [gameData writeToFile:gameStatePath atomically:YES]; 
    [encoder release];

    // Tell the game controller that a resumed game is available
    sharedGameController.resumedGameAvailable = YES;

}

1 个答案:

答案 0 :(得分:0)

有一个使它准备好,设置,去的nstimer,然后如果go没有被隐藏那么你有没有暂停的gameState