2013-08-21 43 views
0

你好,我正在爲我的cocos2d遊戲製作一個暫停屏幕。我有3個按鈕:恢復按鈕,重試按鈕和選項按鈕。他們都沒有迴應觸摸和做他們應該做的事情。當暫停屏幕拉起時,我的所有動作都暫停使用此代碼:cocos2d暫停屏幕中的按鈕不起作用

[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"); 
} 

回答

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,resumeTheGame和optionsMenu

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

你好@ K1laba現在的問題是我的暫停菜單中的按鈕沒有響應觸摸。 – PoKoBros

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 

有了它,你可以暫停遊戲,並添加取消暫停暫停菜單層它上面(我儘量避免替換暫停菜單的場景)。當您取消暫停遊戲時,只需從遊戲層的頂部刪除暫停菜單層即可。