cocos2d-x 游戏暂停界面,监听home键,返回键,Menu键 解决方案

前端之家收集整理的这篇文章主要介绍了cocos2d-x 游戏暂停界面,监听home键,返回键,Menu键 解决方案前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
分类Cocos2d-x@H_403_2@

游戏暂停界面:@H_403_2@

cocos2d-x中游戏暂停界面提供的思路是用pushScene()和popScne(),即推进和弹出场景,当游戏暂停时,推进(pushScene())暂停场景,之前运行的场景将会自动暂停,然后我们可以在暂停场景中操作,如Resume,ReStart,Quit等,当我们不再需要暂停场景时,可以popScene()将暂停场景弹出。(场景就像一张纸,我们推进一个场景,相当于在这张纸上再盖上一张,弹出场景相当于将最表面的那张纸拿掉)。

推进暂停场景的相关代码如下:

  1. CCRenderTexture*renderTexture=CCRenderTexture::create(800,600);@H_403_2@@H_403_2@
  2. renderTexture->begin();@H_403_2@
  3. this@H_403_2@->getParent()->visit();@H_403_2@@H_403_2@
  4. renderTexture->end();//这里实际是通过CCRenderTexture保存当前界面(相当于截屏),然后传递给暂停界面,当成背景精灵@H_403_2@@H_403_2@@H_403_2@
  5. @H_403_2@
  6. CCDirector::sharedDirector()->pushScene(PauseLayer::scene(renderTexture,true@H_403_2@));@H_403_2@@H_403_2@

暂停场景PauseLayer的相关代码如下:
copy
    CCScene*PauseLayer::scene(CCRenderTexture*sqr,@H_403_2@bool@H_403_2@isFlip){@H_403_2@@H_403_2@
  1. CCScene*m_scene=CCScene::create();@H_403_2@
  2. CCSprite*_spr=CCSprite::createWithTexture(sqr->getSprite()->getTexture());@H_403_2@
  3. _spr->setPosition(ccp(400,300));@H_403_2@
  4. _spr->setFlipY(isFlip);@H_403_2@
  5. _spr->setColor(ccGRAY);@H_403_2@
  6. m_scene->addChild(_spr);@H_403_2@
  7. //'layer'isanautoreleaSEObject@H_403_2@@H_403_2@@H_403_2@
  8. PauseLayer*layerr=PauseLayer::create();@H_403_2@
  9. @H_403_2@
  10. //addlayerasachildtoscene@H_403_2@@H_403_2@@H_403_2@
  11. m_scene->addChild(layerr);@H_403_2@
  12. //returnthescene@H_403_2@@H_403_2@@H_403_2@
  13. return@H_403_2@m_scene;@H_403_2@@H_403_2@
  14. }@H_403_2@


监听返回键和Menu键:@H_403_2@

要点:

1.继承CCKeypadDelegate

2.实现两个虚函数

virtual void keyBackClicked()
virtual void keyMenuClicked()

如查要实现监听的对象是CCLayer或者继承CCLayer的,则只需做第二步及在初始化中setKeypadEnabled(true);

因为CCLayer本身继承了CCKeypadDelegate,如下图所示

copy
    class@H_403_2@CC_DLLCCLayer:@H_403_2@public@H_403_2@CCNode,@H_403_2@public@H_403_2@CCTouchDelegate,153); font-weight:bold; background-color:inherit">public@H_403_2@CCAccelerometerDelegate,153); font-weight:bold; background-color:inherit">public@H_403_2@CCKeypadDelegate@H_403_2@@H_403_2@


监听home键:@H_403_2@

在cocos2d-x中我现在还没找到明确的监听home键的方案,但可以用替代方案。

不知你们有没有发现在AppDelegate.cpp里的两个方法

copy
    //Thisfunctionwillbecalledwhentheappisinactive.Whencomesaphonecall,it'sbeinvokedtoo@H_403_2@@H_403_2@@H_403_2@
  1. void@H_403_2@AppDelegate::applicationDidEnterBackground(){@H_403_2@@H_403_2@
  2. CCDirector::sharedDirector()->stopAnimation();@H_403_2@
  3. //ifyouuseSimpleAudioEngine,itmustbepause@H_403_2@@H_403_2@@H_403_2@
  4. //SimpleAudioEngine::sharedEngine()->pauseBackgroundMusic();@H_403_2@@H_403_2@@H_403_2@
  5. }@H_403_2@
  6. //thisfunctionwillbecalledwhentheappisactiveagain@H_403_2@@H_403_2@@H_403_2@
  7. void@H_403_2@AppDelegate::applicationWillEnterForeground(){@H_403_2@@H_403_2@
  8. CCDirector::sharedDirector()->startAnimation();@H_403_2@
  9. 403_2@@H_403_2@@H_403_2@
  10. //SimpleAudioEngine::sharedEngine()->resumeBackgroundMusic();@H_403_2@@H_403_2@@H_403_2@
  11. }@H_403_2@

注意这两个方法的英文解释,实际上这两个方法就是判断程序是否被切换或者说是否被扔至后台工作。因为在手机上按home键,实际就是切换将程序推至后台。So,我们就能在这两个方法文章了。

相关代码如下:

copy
    void@H_403_2@AppDelegate::applicationDidEnterBackground()@H_403_2@@H_403_2@
  1. {@H_403_2@
  2. SimpleAudioEngine::sharedEngine()->pauseBackgroundMusic();@H_403_2@
  3. Global*sh=Global::toIns();@H_403_2@
  4. CCRenderTexture*renderTexture;@H_403_2@
  5. switch@H_403_2@(sh->targetScene){@H_403_2@@H_403_2@
  6. case@H_403_2@TargetSceneFirstScene:@H_403_2@@H_403_2@
  7. break@H_403_2@;@H_403_2@@H_403_2@
  8. case@H_403_2@TargetSceneSecondScene:@H_403_2@@H_403_2@
  9. renderTexture=CCRenderTexture::create(800,600);@H_403_2@
  10. renderTexture->begin();@H_403_2@
  11. sh->battleLayer->visit();@H_403_2@
  12. renderTexture->end();@H_403_2@
  13. CCDirector::sharedDirector()->pushScene(PauseLayer::scene(renderTexture,153); font-weight:bold; background-color:inherit">false@H_403_2@));@H_403_2@@H_403_2@
  14. case@H_403_2@TargetSceneInvalid:@H_403_2@@H_403_2@
  15. default@H_403_2@:@H_403_2@@H_403_2@
  16. }@H_403_2@
  17. //thisfunctionwillbecalledwhentheappisactiveagain@H_403_2@@H_403_2@@H_403_2@
  18. void@H_403_2@AppDelegate::applicationWillEnterForeground()@H_403_2@@H_403_2@
  19. SimpleAudioEngine::sharedEngine()->resumeBackgroundMusic();@H_403_2@
  20. }@H_403_2@

在上面的代码中,我做的是,当程序InActive(推至后台)时,推进暂停界面


如果还有其它的解决方案,欢迎留言

原文链接:/cocos2dx/343080.html

猜你在找的Cocos2d-x相关文章