buttons not working in pause screen in cocos2d

727 views Asked by At

Hello I am making a pause screen for my cocos2d game. I have 3 buttons: the resume button, the retry button, and the options button. None of them are responding to touch and doing what they are supposed to do. When the pause screen is pulled up, all of my actions are paused with this code:

[self pauseSchedulerAndActions];
[[CCDirector sharedDirector]pause];
[[CCDirector sharedDirector] replaceScene:[CCTransitionScene transitionWithDuration:1.0   scene:[PauseMenu scene]]];

This is the code for my pause menu. I want to know why the buttons in the pause screen are not responding to touch and if the resume button and retry button code are correct in what I am trying to do. For the resume button I just want the pause menu layer to disappear and then the CCActions need to resume. For my retry button I just want to have the game restart by calling the GameScene layer which starts the game. Here is the code:

-(id)init{
if((self = [super init])){
CGSize size = [[CCDirector sharedDirector]winSize];
screenWidth = size.width;
screenHeight = size.height;

resumeButton = @"resume.png";
retryButton = @"retry.png";
optionsButton = @"optionspause.png";

pausedLabel = [CCSprite spriteWithFile:@"paused.png"];
pausedLabel.position = ccp(screenWidth/2, screenHeight/1.5);
[self addChild:pausedLabel z:0];

[self makeTheMenu];
}
return self;
}

-(void)makeTheMenu{
CCMenuItem* theResumeButton;
CCMenuItem* theRetryButton;
CCMenuItem* theOptionsButton;

theResumeButton = [CCMenuItemImage itemWithNormalImage:resumeButton    selectedImage:resumeButton target:self selector:@selector(resumeTheGame)];
theResumeButton.scale = 2;
theRetryButton = [CCMenuItemImage itemWithNormalImage:retryButton  selectedImage:retryButton    target:self selector:@selector(retryTheGame)];
theRetryButton.scale = 2;
theOptionsButton = [CCMenuItemImage itemWithNormalImage:optionsButton selectedImage:optionsButton target:self selector:@selector(optionsMenu)];
theOptionsButton.scale = 2;

thePauseMenu = [CCMenu menuWithItems:theResumeButton, theRetryButton, theOptionsButton, nil];
thePauseMenu.position = ccp(screenWidth/2, screenHeight/2 - 100);
[thePauseMenu alignItemsHorizontallyWithPadding:20];
[self addChild:thePauseMenu z:1];
}

-(void)resumeTheGame{
[self resumeSchedulerAndActions];
[[CCDirector sharedDirector]resume];
[thePauseMenu removeFromParentAndCleanup:YES];
}

-(void)retryTheGame{
[[CCDirector sharedDirector] replaceScene:[CCTransitionFade transitionWithDuration:1.0   scene:[GameScene scene] withColor:ccBLACK]]; 
}

-(void)optionsMenu{
CCLOG(@"options");
}
2

There are 2 answers

1
K1laba On BEST ANSWER

Does

[[CCDirector sharedDirector] replaceScene:[CCTransitionScene transitionWithDuration:1.0   scene:[PauseMenu scene]]];

works? You paused director and after this replaceScene.. If it works(I'm not sure).. you changed scene, so it means your GameScene doesn't exist anymore. I would write something like this in my pauseTheGame method:

  PauseMenu *pause = [PauseMenu pauseNode]; // create PauseMenu instance
  [self addChild:pause z:10]; // add pause in your scene;

after this call this in your pauseMenu init method

[self performSelector:@selector(pauseCocos2d) withObject:nil afterDelay:1.0/60];

Here is pauseCocos2d:

-(void)pauseCocos2d
{
    [CCDirector sharedDirector] stopAnimation];
    [[CCDirector sharedDirector]pause];
}

Call this in your retryTheGame, resumeTheGame and optionsMenu

    [CCDirector sharedDirector] startAnimation];
    [[CCDirector sharedDirector]resume];
    // add your line here
0
Ricardo Sanchez-Saez On

When you pause the CCDirector, CCMenus stop working because the cocos2d scheduler is paused. There are different ways of going around this.

What I've been using is this `CCNodep extension:

@implementation CCNode(PauseResume)

- (void)resumeNodeRecursive {
    for (CCNode *child in [self children]) {
        [child resumeNodeRecursive];
    }
    [self resumeSchedulerAndActions];
}

- (void)pauseNodeRecursive {
    [self pauseSchedulerAndActions];
    for (CCNode *child in [self children]) {
        [child pauseNodeRecursive];
    }
}

@end

With it, you can pause the game layer, and add the unpaused pause menu layer above it (I'd try to avoid replacing scenes for a pause menu). When you unpause the game, just remove the pause menu layer from the top of your game layer.