Iphone cocos2d暂停屏幕中的按钮不工作
你好,我正在为我的cocos2d游戏制作一个暂停屏幕。我有3个按钮:恢复按钮、重试按钮和选项按钮。他们中没有人对触摸做出反应,也没有人做他们应该做的事情。当暂停屏幕被拉起时,我的所有操作都会暂停,并显示以下代码:Iphone cocos2d暂停屏幕中的按钮不工作,iphone,objective-c,cocos2d-iphone,resume,cclayer,Iphone,Objective C,Cocos2d Iphone,Resume,Cclayer,你好,我正在为我的cocos2d游戏制作一个暂停屏幕。我有3个按钮:恢复按钮、重试按钮和选项按钮。他们中没有人对触摸做出反应,也没有人做他们应该做的事情。当暂停屏幕被拉起时,我的所有操作都会暂停,并显示以下代码: [self pauseSchedulerAndActions]; [[CCDirector sharedDirector]pause]; [[CCDirector sharedDirector] replaceScene:[CCTransitionScene transitionWit
[self pauseSchedulerAndActions];
[[CCDirector sharedDirector]pause];
[[CCDirector sharedDirector] replaceScene:[CCTransitionScene transitionWithDuration:1.0 scene:[PauseMenu scene]]];
这是我的暂停菜单的代码。我想知道为什么暂停屏幕上的按钮对触摸没有反应,以及恢复按钮和重试按钮代码是否正确。对于“恢复”按钮,我只希望暂停菜单层消失,然后需要恢复操作。对于我的重试按钮,我只想通过调用启动游戏的游戏场景层来重新启动游戏。代码如下:
-(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");
}
做
工作?
您暂停了director并在此场景之后。。
如果它有效(我不确定)。。你们改变了场景,所以这意味着你们的游戏场景不再存在。
我会在我的游戏方法中这样写:
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];
以下是PauseCos2D:
-(void)pauseCocos2d
{
[CCDirector sharedDirector] stopAnimation];
[[CCDirector sharedDirector]pause];
}
在您的重试游戏、恢复游戏和选项菜单中调用此选项
[CCDirector sharedDirector] startAnimation];
[[CCDirector sharedDirector]resume];
// add your line here
暂停
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
使用它,您可以暂停游戏层,并在其上方添加未暂停的暂停菜单层(我会尽量避免将场景替换为暂停菜单)。取消暂停游戏时,只需从游戏层顶部移除暂停菜单层。Hello@K1laba我现在遇到的问题是暂停菜单中的按钮对触摸没有反应。
@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