0

您好,我正在为我的 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");
}
4

2 回答 2

1

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

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

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

在你的 pauseMenu 初始化方法中调用它之后

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

这是pauseCocos2d:

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

在你的 retryTheGame、resumeTheGame 和 optionsMenu 中调用它

    [CCDirector sharedDirector] startAnimation];
    [[CCDirector sharedDirector]resume];
    // add your line here
于 2013-08-22T06:45:01.257 回答
1

当您暂停时CCDirectorCCMenus停止工作,因为 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

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

于 2013-08-22T20:55:02.433 回答