1

I am trying to create pause screen for my game. The game is developed upon continuous scrolling background (top to bottom) with obstacles which are moving independently (left to right).

Below is the code:

PauseScene.h

@interface PauseScene : CCNode {}
@property (nonatomic, retain) HelloWorldScene *mainScene;
//+ (PauseScene *)scene;
- (id) init;
@end

PauseScene.m

@implementation PauseScene

- (id) init {
    if (self = [super init]) {
        // Adding RESUME and QUIT button and few others to make the whole Pause screen
    }
    return self;
}

// Called the method when Resume button is selected
- (void) Resume:(id) sender {
    //[[CCDirector sharedDirector] startAnimation];
    //[[CCDirector sharedDirector] resume];

    self.mainScene.userInteractionEnabled = YES;
    [self.mainScene removeChild: self];
    [self.mainScene StartGame]; // StartGame in MainScene contains the same code which commented out above, I will paste StartGame method later
}

// Quit method is called when the Quit button is pressed
- (void) Quit:(id) sender {
    //[[CCDirector sharedDirector] resume];
    //[[CCDirector sharedDirector] startAnimation];

    self.mainScene.userInteractionEnabled = YES;
    [self.mainScene removeChild: self];
    NSLog(@"Pre Reload", nil);
    //[self.mainScene scheduleOnce:@selector(ReloadGame) delay: 1.0f];
    [self.mainScene ReloadGame]; // Reload method contains the code commented above
}

@end

PauseScene is mere node class.

StartGame and ReloadGame methods in MainScene file

- (void) StartGame {
    // Create and display HUD layer, like, scores, navigation buttons, etc.

    // Resuming the game
    //[[CCDirector sharedDirector] stopAnimation];
    [[CCDirector sharedDirector] resume];
    [[CCDirector sharedDirector] startAnimation];
}

- (void) ReloadGame {
    // Resume the game
    //[[CCDirector sharedDirector] stopAnimation];
    [[CCDirector sharedDirector] resume];
    [[CCDirector sharedDirector] startAnimation];

    [[CCDirector sharedDirector] replaceScene:[HelloWorldScene scene] withTransition:[CCTransition transitionFadeWithDuration:1.0f]];
}

The pause button is displayed in HUD layer of MainScene, so, when the pause button is clicked, we will pause the game and display the PauseScene

- (void)onPauseClicked:(id)sender {
    // Code to display the pause node of this scene

    // Pause the game
    [[CCDirector sharedDirector] stopAnimation];
    [[CCDirector sharedDirector] pause];
}

When I comment out that stopAnimation and pause part the Pause and Resume works perfect. But when I implement any of these, at the time of quitGame, replaceScene is not working somehow. I think this is because game goes in pause mode but it is not working even if I call replaceScene using scheduler on delay of 5-10 seconds.

And if I don't use these stopAnimation and pause, some sprites which are given CCAction are not being paused and goes on even when my game is paused.

Your help is appreciated because I am using Cocos2d 3.0 and unable to find any proper help.

CodeSmile
  • 64,284
  • 20
  • 132
  • 217
Paresh Thakor
  • 1,795
  • 2
  • 27
  • 47
  • start/stopAnimation is not the same as pause, it will just stop making any screen refreshes (so if anything else draws over the cocos2d view you'll get visual glitches). To pause a layer or the scene, set the paused property to YES or only use CCDirector's pause/resume methods. – CodeSmile Jan 16 '15 at 08:18
  • @LearnCocos2D I tried using CCDirector's pause/resume methods but replaceScene is not called. If I remove stopAnimation/startAnimation, then also I am not able to call replaceScene. I also tried calling 'ReloadGame' after delay of 1 sec in another thread considering 'resume' may take sometime and need to execute logic in other block. You can see the commented 'scheduleOnce' method but no output. I don't know if I am missing some basics here, even any good links are appreciated. Thanks man for your help. – Paresh Thakor Jan 16 '15 at 08:41
  • @LearnCocos2D can you give me proper sequencing of these methods? – Paresh Thakor Jan 16 '15 at 08:45
  • @LearnCocos2D the 'paused' property is working fine but it's not stopping actions loaded on sprite using CCActions. My sprite continues to run CCActions event when paused :(. Any suggestion for this? Thanks for the paused property. Need to see difference between property and method on Director :). – Paresh Thakor Jan 16 '15 at 09:04
  • you may want to ask a separate question about this with code examples. A node that's paused really, really should pause its actions, so either the actions aren't running on that node or you may be doing something really odd (like for instance scheduling something with NSTimer or performSelector:afterDelay: perhaps?) – CodeSmile Jan 16 '15 at 09:37
  • thanks it's working now. I come to know that each and every node needs to be paused. – Paresh Thakor Jan 16 '15 at 10:47

1 Answers1

0

Thanks @LearnCocos2D for your tips.

I am able to successfully implement pause/resume by just removing the code stopAnimation or startAnimation and pause and resume methods. I just replaced these all with paused property for the node.

To pause the game, need to set paused = YES for every node in the game and reverse to resume.

Paresh Thakor
  • 1,795
  • 2
  • 27
  • 47