cocos2d-x 游戏暂停界面,监听home键,返回键,Menu键 解决方案
游戏暂停界面:
cocos2d-x中游戏暂停界面提供的思路是用pushScene()和popScne(),即推进和弹出场景,当游戏暂停时,推进(pushScene())暂停场景,之前运行的场景将会自动暂停,然后我们可以在暂停场景中操作,如Resume,ReStart,Quit等,当我们不再需要暂停场景时,可以popScene()将暂停场景弹出。(场景就像一张纸,我们推进一个场景,相当于在这张纸上再盖上一张,弹出场景相当于将最表面的那张纸拿掉)。
推进暂停场景的相关代码如下:
- CCRenderTexture *renderTexture = CCRenderTexture::create(800,600);
- renderTexture->begin();
- this->getParent()->visit();
- renderTexture->end(); //这里实际是通过CCRenderTexture保存当前界面(相当于截屏),然后传递给暂停界面,当成背景精灵
- CCDirector::sharedDirector()->pushScene(PauseLayer::scene(renderTexture,true));
暂停场景PauseLayer的相关代码如下:
- CCScene* PauseLayer::scene(CCRenderTexture* sqr,bool isFlip){
- CCScene *m_scene = CCScene::create();
- CCSprite *_spr = CCSprite::createWithTexture(sqr->getSprite()->getTexture());
- _spr->setPosition(ccp(400, 300));
- _spr->setFlipY(isFlip);
- _spr->setColor(ccGRAY);
- m_scene->addChild(_spr);
- // 'layer' is an autorelease object
- PauseLayer* layerr = PauseLayer::create();
- // add layer as a child to scene
- m_scene->addChild(layerr);
- // return the scene
- return m_scene;
- }
监听返回键和Menu键:
要点:
1.继承CCKeypadDelegate
2.实现两个虚函数
| virtual void | keyBackClicked () |
| virtual void | keyMenuClicked () |
如查要实现监听的对象是CCLayer或者继承CCLayer的,则只需做第二步及在初始化中setKeypadEnabled(true);
setKeypadEnabled(true);
因为CCLayer本身继承了CCKeypadDelegate,如下图所示
- class CC_DLL CCLayer : public CCNode, public CCTouchDelegate, public CCAccelerometerDelegate, public CCKeypadDelegate
监听home键:
在cocos2d-x中我现在还没找到明确的监听home键的方案,但可以用替代方案。
不知你们有没有发现在AppDelegate.cpp里的两个方法:
- // This function will be called when the app is inactive. When comes a phone call,it's be invoked too
- void AppDelegate::applicationDidEnterBackground() {
- CCDirector::sharedDirector()->stopAnimation();
- // if you use SimpleAudioEngine, it must be pause
- // SimpleAudioEngine::sharedEngine()->pauseBackgroundMusic();
- }
- // this function will be called when the app is active again
- void AppDelegate::applicationWillEnterForeground() {
- CCDirector::sharedDirector()->startAnimation();
- // if you use SimpleAudioEngine, it must resume here
- // SimpleAudioEngine::sharedEngine()->resumeBackgroundMusic();
- }
注意这两个方法的英文解释,实际上这两个方法就是判断程序是否被切换或者说是否被扔至后台工作。因为在手机上按home键,实际就是切换将程序推至后台。So,我们就能在这两个方法做文章了。
相关代码如下:
- void AppDelegate::applicationDidEnterBackground()
- {
- CCDirector::sharedDirector()->stopAnimation();
- SimpleAudioEngine::sharedEngine()->pauseBackgroundMusic();
- Global* sh = Global::toIns();
- CCRenderTexture* renderTexture;
- switch(sh->targetScene){
- case TargetSceneFirstScene:
- break;
- case TargetSceneSecondScene:
- renderTexture = CCRenderTexture::create(800,600);
- renderTexture->begin();
- sh->battleLayer->visit();
- renderTexture->end();
- CCDirector::sharedDirector()->pushScene(PauseLayer::scene(renderTexture,false));
- break;
- case TargetSceneInvalid:
- break;
- default:
- break;
- }
- }
- // this function will be called when the app is active again
- void AppDelegate::applicationWillEnterForeground()
- {
- CCDirector::sharedDirector()->startAnimation();
- SimpleAudioEngine::sharedEngine()->resumeBackgroundMusic();
- }
在上面的代码中,我做的是,当程序InActive(推至后台)时,推进暂停界面
cocos2d-x 游戏暂停界面,监听home键,返回键,Menu键 解决方案的更多相关文章
- 微信浏览器返回刷新,监听微信浏览器返回事件,网页防复制,移动端禁止图片长按和vivo手机点击img标签放大图片
以下代码都经过iphone7,华为MT7 ,谷歌浏览器,微信开发者工具,PC端微信验证.如有bug,还请在评论区留言. demo链接:https://pan.baidu.com/s/1c35mbjM ...
- Html5 监听拦截Android返回键方法详解
浏览器窗口有一个history对象,用来保存浏览历史. 如果当前窗口先后访问了三个网址,那么history对象就包括三项,history.length属性等于3. history对象提供了一系列方法, ...
- JavaScript监听手机物理返回键的两种解决方法
JavaScript没有监听物理返回键的API,所以只能使用 popstate 事件监听. 有两个解决办法: 1.返回到指定的页面 pushHistory(); window.addEventList ...
- VueApp监听手机物理返回键的事件
代码 第一步创建js文件夹子 在main里面引用 JS文本内容如下 //监听手机物理返回键的事件 document.addEventListener('plusready', function() ...
- udp 服务器界面监听
今天在做项目的时候,发现不同子网下是不能做UDP通信的,不知道是不是这样呢 遇到一个错误:eclipse遇到报错: The type JPEGImageEncoder is not accessibl ...
- JS监听浏览器的返回、后退、上一页按钮的事件方法
在实际的应用中,我们常常需要实现在移动app和浏览器中点击返回.后退.上一页等按钮实现自己的关闭页面.调整到指定页面或执行一些其它操作的需求,那在代码中怎样监听当点击微信.支付宝.百度糯米.百度钱包等 ...
- 页面返回刷新或H5监听(手机的)返回键
1. pushHistory(); window.addEventListener("popstate", function(e) { alert("我监听到了浏览器的返 ...
- H5新特性监听手机的返回键
var hiddenProperty ='hidden' in document ? 'hidden' :'webkitHidden' in document ? 'webkitHidden' : ' ...
- JS高级——监听浏览器的返回事件
https://www.cnblogs.com/Easty/p/7820055.html https://www.cnblogs.com/zhengyan/p/6912526.html http:// ...
随机推荐
- SQL SERVER 只有MDF文件的恢复
方式一: .create a database same name as .mdf file; .Stop SQL Server; . recover .mdf file; . Start SQL S ...
- mysql.server 文件修改起动多实例
如果你用mysql.server来启动的话,就要注意一下点,要修改一下里面的内容,修改如下:大约218行左右 查看复制打印? then print_defaults="$bindir/mys ...
- 转:java日志组件介绍(common-logging,log4j,slf4j,logback )
原网址:http://www.blogjava.net/daiyongzhi/archive/2014/04/13/412364.html common-logging common-logging是 ...
- [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 ...
- Python基础教程【读书笔记】 - 2016/7/5
希望通过博客园持续的更新,分享和记录Python基础知识到高级应用的点点滴滴! 第三波:第8章 异常 [总览]学习如何创建和引发自定义的异常,以及处理异常的各种方法. 为了能够处理异常事件,可以再所 ...
- CSS position, z-index
position 1.fixed:定位.浮动.(需要搭配left, right) 2.absolute:相对于最近的父元素,不考虑周围的布局.(可使用z-index占据位置,则同一位置层叠) 3.re ...
- 剑指offer系列33-----把二叉树打印成多行
[题目]从上到下按层打印二叉树,同一层结点从左至右输出.每一层输出一行. 方法一:直接打印 package com.exe7.offer; import java.util.LinkedList; i ...
- Python标准库
Python标准库是随Python附带安装的,它包含大量极其有用的模块.熟悉Python标准库是十分重要的,因为如果你熟悉这些库中的模块,那么你的大多数问题都可以简单快捷地使用它们来解决. sys模块 ...
- WSUS目录本地迁移
生产环境中有一台win2003 server,安装了Microsoft Windows Server Update Services 3.0,作为所有windows server的内网补丁更新服务器, ...
- python学习-day11-内建函数
python-内建函数 -int:将字符串转换为数字 a = " print(type(a),a) b = int(a) print(type(b),b) num = " v = ...