嗯...我從來沒有使用[CCDirector sharedDirector]暫停。它暫停了所有的Cocos2D,這不是你想要的。我不清楚的是如果你想要一個接管屏幕的菜單,或者如果你想在遊戲之上有一個菜單,但你仍然可以看到遊戲。
添加全屏幕菜單:
創建一個新的子類CCLayer並將其命名爲任何你想要的。將它設置你喜歡它,然後使用:
// 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];
添加一個覆蓋式菜單:
爲了得到一個菜單,你仍然可以看到遊戲下方你會想要做這樣的事情。
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];
然後您需要有一個標誌,您的遊戲可以檢查以確定它是否應該執行任何操作。
例如,如果你有一個方法,更新您的AI做的事情:(ccTime)DT那麼你可以這樣做:
// Check to see if game actions should be running or not
if(!isGamePaused)
{
// AI Code here
}
您提供給任何該標誌需要它並檢查暫停旗。在上面的代碼中,isGamePaused是一個布爾類型的變量。
如果您需要暫停像Box2D這樣的物理模擬,這也適用於此。
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());
}
}
}
希望有幫助。
哦,是的,當你需要你的遊戲恢復時,不要忘記設置標誌爲假。 –