游戏暂停界面:

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. CCDirector::sharedDirector()->pushScene(PauseLayer::scene(renderTexture,true));

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

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

监听返回键和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. // if you use SimpleAudioEngine, it must be pause
  5. // SimpleAudioEngine::sharedEngine()->pauseBackgroundMusic();
  6. }
  7. // this function will be called when the app is active again
  8. void AppDelegate::applicationWillEnterForeground() {
  9. CCDirector::sharedDirector()->startAnimation();
  10. // if you use SimpleAudioEngine, it must resume here
  11. // SimpleAudioEngine::sharedEngine()->resumeBackgroundMusic();
  12. }

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

相关代码如下:

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

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

cocos2d-x 游戏暂停界面,监听home键,返回键,Menu键 解决方案的更多相关文章

  1. 微信浏览器返回刷新,监听微信浏览器返回事件,网页防复制,移动端禁止图片长按和vivo手机点击img标签放大图片

    以下代码都经过iphone7,华为MT7 ,谷歌浏览器,微信开发者工具,PC端微信验证.如有bug,还请在评论区留言. demo链接:https://pan.baidu.com/s/1c35mbjM ...

  2. Html5 监听拦截Android返回键方法详解

    浏览器窗口有一个history对象,用来保存浏览历史. 如果当前窗口先后访问了三个网址,那么history对象就包括三项,history.length属性等于3. history对象提供了一系列方法, ...

  3. JavaScript监听手机物理返回键的两种解决方法

    JavaScript没有监听物理返回键的API,所以只能使用 popstate 事件监听. 有两个解决办法: 1.返回到指定的页面 pushHistory(); window.addEventList ...

  4. VueApp监听手机物理返回键的事件

    代码 第一步创建js文件夹子 在main里面引用   JS文本内容如下 //监听手机物理返回键的事件 document.addEventListener('plusready', function() ...

  5. udp 服务器界面监听

    今天在做项目的时候,发现不同子网下是不能做UDP通信的,不知道是不是这样呢 遇到一个错误:eclipse遇到报错: The type JPEGImageEncoder is not accessibl ...

  6. JS监听浏览器的返回、后退、上一页按钮的事件方法

    在实际的应用中,我们常常需要实现在移动app和浏览器中点击返回.后退.上一页等按钮实现自己的关闭页面.调整到指定页面或执行一些其它操作的需求,那在代码中怎样监听当点击微信.支付宝.百度糯米.百度钱包等 ...

  7. 页面返回刷新或H5监听(手机的)返回键

    1. pushHistory(); window.addEventListener("popstate", function(e) { alert("我监听到了浏览器的返 ...

  8. H5新特性监听手机的返回键

    var hiddenProperty ='hidden' in document ? 'hidden' :'webkitHidden' in document ? 'webkitHidden' : ' ...

  9. JS高级——监听浏览器的返回事件

    https://www.cnblogs.com/Easty/p/7820055.html https://www.cnblogs.com/zhengyan/p/6912526.html http:// ...

随机推荐

  1. SQL SERVER 只有MDF文件的恢复

    方式一: .create a database same name as .mdf file; .Stop SQL Server; . recover .mdf file; . Start SQL S ...

  2. mysql.server 文件修改起动多实例

    如果你用mysql.server来启动的话,就要注意一下点,要修改一下里面的内容,修改如下:大约218行左右 查看复制打印? then print_defaults="$bindir/mys ...

  3. 转:java日志组件介绍(common-logging,log4j,slf4j,logback )

    原网址:http://www.blogjava.net/daiyongzhi/archive/2014/04/13/412364.html common-logging common-logging是 ...

  4. [oracle] 解决X64操作系统PL/SQL连接报错问题 make sure you have the 32 bits oracle client installed

    Windows 64位下装Oracle 11g 64位,PLSQL Developer使用出现以下问题: 1.Database下拉框为空: 2.强制输入用户名.密码及Database,登录弹出: In ...

  5. Python基础教程【读书笔记】 - 2016/7/5

    希望通过博客园持续的更新,分享和记录Python基础知识到高级应用的点点滴滴! 第三波:第8章  异常 [总览]学习如何创建和引发自定义的异常,以及处理异常的各种方法. 为了能够处理异常事件,可以再所 ...

  6. CSS position, z-index

    position 1.fixed:定位.浮动.(需要搭配left, right) 2.absolute:相对于最近的父元素,不考虑周围的布局.(可使用z-index占据位置,则同一位置层叠) 3.re ...

  7. 剑指offer系列33-----把二叉树打印成多行

    [题目]从上到下按层打印二叉树,同一层结点从左至右输出.每一层输出一行. 方法一:直接打印 package com.exe7.offer; import java.util.LinkedList; i ...

  8. Python标准库

    Python标准库是随Python附带安装的,它包含大量极其有用的模块.熟悉Python标准库是十分重要的,因为如果你熟悉这些库中的模块,那么你的大多数问题都可以简单快捷地使用它们来解决. sys模块 ...

  9. WSUS目录本地迁移

    生产环境中有一台win2003 server,安装了Microsoft Windows Server Update Services 3.0,作为所有windows server的内网补丁更新服务器, ...

  10. python学习-day11-内建函数

    python-内建函数 -int:将字符串转换为数字 a = " print(type(a),a) b = int(a) print(type(b),b) num = " v = ...