zoukankan      html  css  js  c++  java
  • cocos2d-x 游戏暂停界面,监听home键,返回键,Menu键 解决方案

    游戏暂停界面:

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

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

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


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

    1. CCScene* PauseLayer::scene(CCRenderTexture* sqr,bool isFlip){  
    2.     CCScene *m_scene = CCScene::create();  
    3.   
    4.     CCSprite *_spr = CCSprite::createWithTexture(sqr->getSprite()->getTexture());  
    5.     _spr->setPosition(ccp(400, 300));  
    6.     _spr->setFlipY(isFlip);  
    7.     _spr->setColor(ccGRAY);  
    8.     m_scene->addChild(_spr);  
    9.   
    10.     // 'layer' is an autorelease object  
    11.     PauseLayer* layerr = PauseLayer::create();  
    12.   
    13.     // add layer as a child to scene  
    14.     m_scene->addChild(layerr);  
    15.   
    16.   
    17.   
    18.     // return the scene  
    19.     return m_scene;  
    20. }  



    监听返回键和Menu键:

    要点:

    1.继承CCKeypadDelegate

    2.实现两个虚函数

    virtual void  keyBackClicked ()
     
    virtual void  keyMenuClicked ()

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

    setKeypadEnabled(true);

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

    1. class CC_DLL CCLayer : public CCNode, public CCTouchDelegate, public CCAccelerometerDelegate, public CCKeypadDelegate  



    监听home键:

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

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

    1. // This function will be called when the app is inactive. When comes a phone call,it's be invoked too  
    2. void AppDelegate::applicationDidEnterBackground() {  
    3.     CCDirector::sharedDirector()->stopAnimation();  
    4.   
    5.     // if you use SimpleAudioEngine, it must be pause  
    6.     // SimpleAudioEngine::sharedEngine()->pauseBackgroundMusic();  
    7. }  
    8.   
    9. // this function will be called when the app is active again  
    10. void AppDelegate::applicationWillEnterForeground() {  
    11.     CCDirector::sharedDirector()->startAnimation();  
    12.   
    13.     // if you use SimpleAudioEngine, it must resume here  
    14.     // SimpleAudioEngine::sharedEngine()->resumeBackgroundMusic();  
    15. }  


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

    相关代码如下:

    1. void AppDelegate::applicationDidEnterBackground()  
    2. {  
    3.     CCDirector::sharedDirector()->stopAnimation();  
    4.   
    5.     SimpleAudioEngine::sharedEngine()->pauseBackgroundMusic();  
    6.     Global* sh = Global::toIns();  
    7.     CCRenderTexture* renderTexture;  
    8.     switch(sh->targetScene){  
    9.     case TargetSceneFirstScene:  
    10.         break;  
    11.     case TargetSceneSecondScene:  
    12.         renderTexture = CCRenderTexture::create(800,600);  
    13.         renderTexture->begin();  
    14.         sh->battleLayer->visit();  
    15.         renderTexture->end();  
    16.   
    17.         CCDirector::sharedDirector()->pushScene(PauseLayer::scene(renderTexture,false));  
    18.         break;  
    19.     case TargetSceneInvalid:  
    20.         break;  
    21.     default:  
    22.         break;  
    23.     }  
    24. }  
    25.   
    26. // this function will be called when the app is active again  
    27. void AppDelegate::applicationWillEnterForeground()  
    28. {  
    29.     CCDirector::sharedDirector()->startAnimation();  
    30.   
    31.     SimpleAudioEngine::sharedEngine()->resumeBackgroundMusic();  
    32. }  


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

  • 相关阅读:
    喜欢的诗
    诗集与集诗
    oracle 12c 中asm元数据是否有所变化
    hdu2066一个人的旅行(dijkstra)
    单链表
    ExtJS4.2学习(7)——基础知识之Reader&Writer篇
    hdu3790最短路径问题 (用优先队列实现的)
    poj 1220 NUMBER BASE CONVERSION(短除法进制转换)
    POJ 4003 Bob’s Race && HDU4123 Bob’s Race (dfs+rmq)
    全排列
  • 原文地址:https://www.cnblogs.com/ADaii/p/3442128.html
Copyright © 2011-2022 走看看