在cocos2d中恢复游戏循环

时间:2012-08-17 18:53:10

标签: cocos2d-iphone resume

我有一个动画游戏。在游戏场景中,我使用下面的方法暂停我的游戏(它完美运行,因为当用户点击iOS设备的任何一点时游戏暂停):

-(void)ccTouchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    UITouch *touch=[touches anyObject];
    CGPoint locationOfTouch=[self convertTouchToNodeSpace: touch];

    CGRect rectOfScreen=CGRectMake(0, 0, screenSize.width, screenSize.height);

if (CGRectContainsPoint(rectOfScreen, locationOfTouch)) 
{
    [[CCDirector sharedDirector] replaceScene:[PauseScene scene]];
    [self unschedule:@selector(spawnEnemies:)];
    [self unschedule:@selector(checkCollisionOfEnemyWithBullet:)];
    [self unschedule:@selector(update:)];
    [self unschedule:@selector(isJoystickActivated:)];
    [self unschedule:@selector(checkHasGameEnded:)];

    [[CCDirector sharedDirector] pause];
}

}

为了实现恢复操作,我创建了PauseLayer:CCLayer,我已经实现了以下方法来恢复游戏:

-(void) continueGame:(id)sender
{
[[CCDirector sharedDirector] resume];
[[CCDirector sharedDirector] replaceScene:[GameSceneLayer scene]];
//[self resumeSchedulerAndActions];
}

这是代码,我如何调用上面的方法:

 CCMenuItemFont* continue_game=[CCMenuItemFont itemWithString:@"continue game" target:self selector:@selector(continueGame:)];

但是,当我选择继续游戏时,游戏从空白点开始:每个游戏状态,每个游戏角色都是新的。如何从用户暂停的初始点恢复我的游戏?谢谢!

1 个答案:

答案 0 :(得分:0)

您可以使用这两个cocos2D功能暂停和恢复:

[self pauseSchedulerAndActions]; //pause
[self resumeSchedulerAndActions];//resume

还要保留一个bool变量,并在需要时检查它。

   bool mISGamePaused;