When pausing a layer in cocos2d menu doesn't respond?
I was trying to create a gameover screen. It would create a new layer and place it on top of the current game layer and have a menu which would allow you to go home. But the menu doesn't respond after I pause all actions, any way around this? Thanks!
//Pause all actions
[[CCDirector share开发者_如何学GodDirector] pause];
//Create new black layer
CCLayerColor *gameover = [CCLayerColor layerWithColor:ccc4(0, 0, 0, 75)];
[self addChild:gameover z:1];
//add menu
CCMenuItemFont *item1 = [CCMenuItemFont itemFromString:@"Continue" target:self selector:@selector(goToMainMenu)];
CCMenu *menu = [CCMenu menuWithItems:item1, nil];
[gameover addChild:menu z:110];
EDIT: I am trying to create a transparent layer on top
Pausing the shared director pauses everything... I just asked this yesterday:
Is there anyway to have a the menu work even when I pause the game?
You can do what the guy did and also another method I found which was just inactivating all your scheduled selectors (i.e. [self unschedule:@selector(gameLogicLoop:)];
)
And then you can reschedule the stuff after you are done clicking the menu ([self schedule:@selector(gameLogicLoop:)];
).
I just wrapped everything in a BOOL and it worked fine for me.
BOOL paused;
-(void)goToMainMenu
{
if (paused == YES)
{
[[CCDirector sharedDirector] replaceScene:[MainMenu scene]];
}
}
-(void)gameOver
{
paused = YES;
[[CCDirector sharedDirector] pause]];
}
-(id) init
{
if((self=[super init]))
{
paused = NO;
[self gameOver];
}
return self;
}
EDIT:
Why are you adding the CCMenu to the CCLayerColor? Just add it to the CCLayer.
[self addChild:menu];
Hope this helps!
-(void)MenuStatus:(BOOL)_enable Node:(id)_node
{
for (id result in ((CCNode *)_node).children) {
if ([result isKindOfClass:[CCMenu class]]) {
for (id result1 in ((CCMenu *)result).children) {
if ([result1 isKindOfClass:[CCMenuItem class]]) {
((CCMenuItem *)result1).isEnabled = _enable;
}
}
}
else
[self MenuStatus:_enable Node:result];
}
}
and then Call like
[self MenuStatus:YES Node:self]; //YES to Enable or NO to Disable the layer specified
CCSprite *background = [CCSprite spriteWithFile:@"background3.png"];
//[background initWithFile:@"background3.png"];
[background setPosition:ccp(screenSize.width/2, screenSize.height/2)];
[self addChild:background z:1];
[background setOpacity:420];
To make it FadeIn, this should work:
CCLayerColor _shadowLayer = [CCLayerColor layerWithColor: ccc4(0,0,0, 0)];
[_shadowLayer setContentSize: CGSizeMake(_winSize.width, _winSize.height)];
_shadowLayer.anchorPoint = ccp(0.0f, 0.0f);
_shadowLayer.position = ccp(0, 0);
[self addChild: _shadowLayer];
[_shadowLayer runAction: [CCFadeTo actionWithDuration:1.5f opacity:100]];
You can also get what you want by the following approach
-(void) pause:(ccTime)dt
{
NSLog(@"Pausing");
CCDirector *director = [CCDirector sharedDirector];
self.pausedTargets = [director.actionManager pauseAllRunningActions];
}
-(void) resume:(ccTime)dt
{
NSLog(@"Resuming");
CCDirector *director = [CCDirector sharedDirector];
[director.actionManager resumeTargets:self.pausedTargets];
}
精彩评论