有没有反正有菜单工作,即使我暂停游戏? [英] Is there anyway to have a the menu work even when I pause the game?

查看:112
本文介绍了有没有反正有菜单工作,即使我暂停游戏?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我正在创建一个菜单:

  CCMenu * menu = [CCMenu menuWithItems:addThing1,addThing2,addThing3,nil] ; 
[menu alignItemsVertically];
[self addChild:menu];

它工作正常,但我也想暂停游戏,

  [[CCDirector sharedDirector] pause]; 

所以,这也有效,但问题是当我暂停游戏时,太。有没有反正这个不会发生?在我看来,它的反直觉,暂停菜单太...但无论如何:有没有办法使它不暂停菜单?或者我必须诉诸其他方法...如果我不得不,但想知道是否有任何相对快速修复这个代码,将使其工作...

解决方案

好吧...我从来不使用[[CCDirector sharedDirector] pause]。它暂停所有的Cocos2D,这不是你想要的在这里。我不清楚的是,如果你想要一个菜单​​,接管了屏幕,或者如果你想要一个菜单​​的游戏,但你仍然可以看到游戏。



添加全屏菜单



创建一个新的CCLayer子类,并为其命名。设置它如何你喜欢它,然后使用:

  //推动PauseLayer屏幕(或任何你称为菜单
// layer class)
//这将暂停你的游戏,并保持它的内存状态。
[[CCDirector sharedDirector] pushScene:[PauseLayer node]];

//弹出缓存的场景
//这将删除你的菜单层,并显示你的
//游戏层。
//任何自定义计时器可能需要
//复位/恢复onEnter或onEnterTransitionDidFinish
[[CCDirector sharedDirector] popScene];

添加叠加层类型菜单



要获得菜单,您仍然可以看到下面的游戏,您将需要这样做。

  CCLabelBMFont * difficultyLabel = [CCLabelBMFont labelWithString:@DifficultyfntFile:@projectOneMenuItem1.fnt]; 
CCMenuItemLabel * difficulty = [CCMenuItemLabel itemWithLabel:difficultyLabel target:self selector:@selector(chooseDifficulty :)];
CCLabelBMFont * audioSourceLabel = [CCLabelBMFont labelWithString:@切换到iPod音频fntFile:@projectOneMenuItem1.fnt];
CCMenuItemLabel * audioSource = [CCMenuItemLabel itemWithLabel:audioSourceLabel target:self selector:@selector(switchAudioSource :)];
CCLabelBMFont * leaderboardsLabel = [CCLabelBMFont labelWithString:@LeaderboardsfntFile:@projectOneMenuItem1.fnt];
CCMenuItemLabel * leaderboards = [CCMenuItemLabel itemWithLabel:leaderboardsLabel target:self selector:@selector(showLeaderboards :)];
CCLabelBMFont * achievementLabel = [CCLabelBMFont labelWithString:@AchievementsfntFile:@projectOneMenuItem1.fnt];
CCMenuItemLabel * achievement = [CCMenuItemLabel itemWithLabel:achievementsLabel target:self selector:@selector(showAchievements :)];
CCLabelBMFont * backLabel = [CCLabelBMFont labelWithString:@BackfntFile:@projectOneMenuItem1.fnt];
CCMenuItemLabel * back = [CCMenuItemLabel itemWithLabel:backLabel target:self selector:@selector(goBack :)];

CCMenu * menu = [CCMenu menuWithItems:difficulty,audioSource,leaderboards,achievement,back,nil];

menu.position = ccp(winSize.width / 2,winSize.height * 0.15);
[menu alignItemsVerticallyWithPadding:10];
[self addChild:menu];

然后您需要有一个标志,您的游戏可以检查以决定是否应该执行任何操作



例如,如果您的AI在方法更新中执行某项操作:(ccTime)dt,那么您将这样做:

  //检查游戏操作是否应该运行
if(!isGamePaused)
{
// AI这里的代码
}

你使这个标志可用于任何需要的标志,暂停标志。在上面的代码中,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());
}
}
}

希望有帮助。 p>

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

解决方案

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.

这篇关于有没有反正有菜单工作,即使我暂停游戏?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆