按钮在cocos2d的暂停屏幕中不起作用

时间:2013-08-21 20:25:45

标签: iphone objective-c cocos2d-iphone resume cclayer

您好我正在为我的cocos2d游戏制作一个暂停屏幕。我有3个按钮:恢复按钮,重试按钮和选项按钮。他们都没有回应触摸和做他们应该做的事情。当拉出暂停屏幕时,我的所有操作都会暂停此代码:

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

这是我暂停菜单的代码。我想知道为什么暂停屏幕中的按钮没有响应触摸,以及恢复按钮和重试按钮代码是否正确我正在尝试做什么。对于恢复按钮,我只想让暂停菜单层消失,然后CCActions需要恢复。对于我的重试按钮,我只想通过调用启动游戏的GameScene层来重启游戏。这是代码:

-(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 个答案:

答案 0 :(得分:1)

确实

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

的作品? 你停了导演,然后替换了剧情。 如果它有效(我不确定)..你改变了场景,所以这意味着你的GameScene不再存在。 我会在pauseTheGame方法中写出这样的东西:

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

之后在pauseMenu init方法中调用它

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

这是pauseCocos2d:

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

在你的retryTheGame中调用它,恢复游戏和选项菜单

    [CCDirector sharedDirector] startAnimation];
    [[CCDirector sharedDirector]resume];
    // add your line here

答案 1 :(得分:1)

暂停CCDirector时,CCMenus停止工作,因为cocos2d调度程序已暂停。有不同的解决方法。

我一直在使用的是这个`CCNodep扩展名:

@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

有了它,您可以暂停游戏图层,并在其上方添加未暂停的暂停菜单图层(我会尝试避免替换暂停菜单的场景) 。当您取消暂停游戏时,只需从游戏图层的顶部删除暂停菜单图层

相关问题