Reputation: 16240
So I am making a menu:
CCMenu *menu = [CCMenu menuWithItems:addThing1, addThing2, addThing3, nil];
[menu alignItemsVertically];
[self addChild:menu];
It works fine, but I also want to pause the game when this happens, so I do this:
[[CCDirector sharedDirector] pause];
So, this works too, but the problem is that when I pause the game, it pauses the menu too. Is there anyway for this not to happen? It seems to me that its counter intuitive to pause the menu too... But anyways: Is there a way to make it not pause the menu? Or do I have to resort to other methods... I will if I have to but want to know if there is any relatively quick fix to this code that will make it work...
Upvotes: 2
Views: 815
Reputation: 634
Well... I never use [[CCDirector sharedDirector] pause]. It pauses all of Cocos2D and that isn't what you want here. What I am not clear on is if you want a menu that takes over the screen, or if you want a menu on top of the game but you can still see the game.
Add full screen menu:
Create a new CCLayer subclass and name it whatever you want. Set it up how you like it and then use:
// Push the PauseLayer screen (or whatever you have called your menu
// layer class)
// This will pause your game and hold it's state in memory.
[[CCDirector sharedDirector] pushScene:[PauseLayer node]];
// Pop the cached scene
// This will remove your menu layer and display your
// game layer where it was left off.
// Any custom timers will likely need to be
// reset/resumed inside of onEnter or onEnterTransitionDidFinish
[[CCDirector sharedDirector] popScene];
Add a overlay type menu:
To get a menu where you can still see the game underneath you will want to do something like this.
CCLabelBMFont *difficultyLabel = [CCLabelBMFont labelWithString:@"Difficulty" fntFile:@"projectOneMenuItem1.fnt"];
CCMenuItemLabel *difficulty = [CCMenuItemLabel itemWithLabel:difficultyLabel target:self selector:@selector(chooseDifficulty:)];
CCLabelBMFont *audioSourceLabel = [CCLabelBMFont labelWithString:@"Switch to iPod Audio" fntFile:@"projectOneMenuItem1.fnt"];
CCMenuItemLabel *audioSource = [CCMenuItemLabel itemWithLabel:audioSourceLabel target:self selector:@selector(switchAudioSource:)];
CCLabelBMFont *leaderboardsLabel = [CCLabelBMFont labelWithString:@"Leaderboards" fntFile:@"projectOneMenuItem1.fnt"];
CCMenuItemLabel *leaderboards = [CCMenuItemLabel itemWithLabel:leaderboardsLabel target:self selector:@selector(showLeaderboards:)];
CCLabelBMFont *achievementsLabel = [CCLabelBMFont labelWithString:@"Achievements" fntFile:@"projectOneMenuItem1.fnt"];
CCMenuItemLabel *achievements = [CCMenuItemLabel itemWithLabel:achievementsLabel target:self selector:@selector(showAchievements:)];
CCLabelBMFont *backLabel = [CCLabelBMFont labelWithString:@"Back" fntFile:@"projectOneMenuItem1.fnt"];
CCMenuItemLabel *back = [CCMenuItemLabel itemWithLabel:backLabel target:self selector:@selector(goBack:)];
CCMenu *menu = [CCMenu menuWithItems: difficulty, audioSource, leaderboards, achievements, back, nil];
menu.position = ccp(winSize.width/2, winSize.height*0.15);
[menu alignItemsVerticallyWithPadding:10];
[self addChild:menu];
You will then need to have a flag that your game can check to decide if it should do any actions or not.
For example if you had your AI doing something in a method update:(ccTime)dt then you would do this:
// Check to see if game actions should be running or not
if(!isGamePaused)
{
// AI Code here
}
You make this flag available to anything that needs it and check for the pause flag. In the code above, isGamePaused is a bool variable available class wide.
If you need to pause a physics simulation like Box2D, this works for that as well.
if(!isGamePaused)
{
_world->Step(dt, 10, 10);
for(b2Body *b = _world->GetBodyList(); b; b=b->GetNext())
{
if (b->GetUserData() != NULL)
{
if(!b->IsActive()) continue;
CCSprite *sprite = (CCSprite *)b->GetUserData();
sprite.position = ccp(b->GetPosition().x * PTM_RATIO, b->GetPosition().y * PTM_RATIO);
sprite.rotation = -1 * CC_RADIANS_TO_DEGREES(b->GetAngle());
}
}
}
Hopefully that helps.
Upvotes: 2
Reputation: 25692
if i develop ur app i will be do like this
instead of pause the game i will create the custom scene with ur menu.
instead of pausing the game i will do replace the previous scene by this new scene which has already we have added the menu in the previous one.
so it accessible even we pause the game.(pause means faded new scene with ur menu )
Upvotes: 0