Iphone 即使在我暂停游戏的时候,菜单上是否也有一个菜单?
所以我在做菜单:Iphone 即使在我暂停游戏的时候,菜单上是否也有一个菜单?,iphone,cocos2d-iphone,Iphone,Cocos2d Iphone,所以我在做菜单: CCMenu *menu = [CCMenu menuWithItems:addThing1, addThing2, addThing3, nil]; [menu alignItemsVertically]; [self addChild:menu]; 它工作正常,但发生这种情况时我也想暂停游戏,所以我这样做: [[CCDirector sharedDirector] pause]; 这也行,但问题是当我暂停游戏时,它也会暂停菜单。有没有办法避免这种情况发生?在我看来,暂停
CCMenu *menu = [CCMenu menuWithItems:addThing1, addThing2, addThing3, nil];
[menu alignItemsVertically];
[self addChild:menu];
它工作正常,但发生这种情况时我也想暂停游戏,所以我这样做:
[[CCDirector sharedDirector] pause];
这也行,但问题是当我暂停游戏时,它也会暂停菜单。有没有办法避免这种情况发生?在我看来,暂停菜单也是违反直觉的。。。但无论如何,有没有办法让它不暂停菜单?还是我必须求助于其他方法。。。如果有必要,我会的,但我想知道是否有任何相对快速的修复程序可以使代码正常工作…如果我开发你的应用程序,我会这样做 我不会暂停游戏,而是用菜单创建自定义场景 不是暂停游戏,我会用这个新的场景替换上一个场景,我们已经在上一个场景中添加了菜单
因此,即使我们暂停游戏,也可以使用它。(暂停意味着你的菜单中出现了褪色的新场景)嗯。。。我从不使用[[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是一个在类范围内可用的bool变量
如果需要暂停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());
}
}
}
希望这能有所帮助。哦,是的,当你需要恢复游戏时,别忘了将标志设置为false。