即使我暂停游戏,菜单是否仍然有效?

Is there anyway to have a the menu work even when I pause the game?(即使我暂停游戏,菜单是否仍然有效?)

本文介绍了即使我暂停游戏,菜单是否仍然有效?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我正在制作一个菜单:

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];

所以,这也可以,但问题是当我暂停游戏时,它也会暂停菜单.无论如何,这不会发生吗?在我看来,暂停菜单也是反直觉的......但无论如何:有没有办法让它不暂停菜单?还是我必须求助于 other 方法...如果必须,我会,但想知道是否有任何相对快速的修复此代码可以使其工作...

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...

推荐答案

嗯...我从不使用 [[CCDirector sharedDirector] pause].它暂停了所有的 Cocos2D,这不是你想要的.我不清楚的是你是否想要一个占据屏幕的菜单,或者你想要一个菜单​​在游戏顶部但你仍然可以看到游戏.

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.

添加全屏菜单:

创建一个新的 CCLayer 子类并将其命名为您想要的任何名称.设置你喜欢的方式然后使用:

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];

添加叠加类型菜单:

要获得一个仍然可以在下面看到游戏的菜单,您需要执行以下操作.

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.

例如,如果你让你的 AI 在方法 update:(ccTime)dt 中做某事,那么你会这样做:

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
}

您将此标志提供给任何需要它的东西并检查暂停标志.在上面的代码中,isGamePaused 是一个类范围内可用的布尔变量.

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.

如果您需要暂停 Box2D 等物理模拟,这也适用.

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());
        }        
    }
}

希望对您有所帮助.

这篇关于即使我暂停游戏,菜单是否仍然有效?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本文标题为:即使我暂停游戏,菜单是否仍然有效?

基础教程推荐