游戏暂停界面:

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(推至后台)时,推进暂停界面

最新文章

  1. NHibernate使用Access数据库的配置问题
  2. 在ASP.NET中上传附件
  3. windows API 开发飞机订票系统 图形化界面 (一)
  4. UnrealEngine4 PBR Shading Model 概述
  5. C++:常类型Const
  6. LightOJ 1422 Halloween Costumes
  7. YUV转灰度
  8. iOS开发总结-图片左右滑动浏览
  9. CentOS 7 之Shell学习笔记
  10. scriptol图像处理算法
  11. python之pygal:掷两个不同的骰子并统计大小出现次数
  12. python 中的map,dict,lambda,reduce,filter
  13. Hexo+Github 搭建属于自己的博客(Mac下安装 其他操作系统大同小异)
  14. c# 得到list符合某条件的索引值,排序
  15. Linux命令: 在线练习网址
  16. PHP:第三章——PHP中的可变函数
  17. C语言作业03-函数
  18. 升级salt导致进程kill问题记录
  19. plsql 只有三个文本框,无法登陆
  20. 迭代获取ViewState

热门文章

  1. Jquary获取页面控件的值
  2. web前端-面试经验总结
  3. sklearn基础知识-准备阶段
  4. 一个完整的JENKINS下的ANT BUILD.XML文件(Jenkins可以参考)
  5. CSharp 调用存储过程来执行增、删、改操作
  6. Jfinal连接自助数据库的数据源
  7. linux下openoffice的安装和启动
  8. ajax简单案例:字符串返回类型
  9. 05文件与IO
  10. Linux的软连接与硬链接