返回按钮和目录按钮的监听

来源:互联网 发布:mac 休眠后断网 编辑:程序博客网 时间:2024/05/21 04:15

http://www.firedragonpzy.com.cn/index.php/archives/2767

转载自【Softeware MyZone】原文链接: http://www.firedragonpzy.com.cn/index.php/archives/2767

欢迎热爱编程的朋友们参与到cocos2d-x编程中,为了给大家提供良好的交流环境,网站以开启QQ群
Software MyZone:66202765(群号,欢迎加入,若满,请加1群)
Software MyZone 1群(2dx):286504621
【加群请写:Software MyZone或者是firedragonpzy】
群论坛正在筹建中,欢迎大家多提些建设性意见……

之前写了Box2d物体移动的例子,没有分享一下,最近规整了一下代码,与大家分享。。。
最近做android的游戏,需要监听android的菜单和返回按钮,记得1.x的时候貌似还是比较复杂,没想到2.x的竟然如此简单,现在与大家分享下,一直跟随博文的同志们就不用再从网上search了。
首先,声明函数:

?
1
2
virtualvoidkeyBackClicked();//监听Android 返回键
virtualvoidkeyMenuClicked();//监听Android 菜单键

在cpp中使用的时候记得开启监听事件:

?
1
this->setKeypadEnabled(true);

就这么简单…………
这个是使用了CCKeypadDelegate,咱们之所以能用,是因为大家的类继承了CCLayer,CCLayer是这么声明的:

?
1
classCC_DLL CCLayer : publicCCNode,publicCCTouchDelegate,publicCCAccelerometerDelegate,publicCCKeypadDelegate{……}

它继承了CCKeypadDelegate代理,代理是这样子的:

?
1
2
3
4
5
6
7
8
9
classCC_DLL CCKeypadDelegate
{
public:
 // The back key clicked
 virtualvoidkeyBackClicked() {}
 
// The menu key clicked. only avialble on wophone & android
 virtualvoidkeyMenuClicked() {};
};

好了,有关android按钮的监听事件就说到这里吧,呼呼……

、、、、、、、、、、、、、、、、、、、

http://blog.csdn.net/we000636/article/details/8597540


 

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

分类: Cocos2d-x 3560人阅读 评论(4) 收藏 举报

游戏暂停界面:

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

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

[cpp] view plaincopy
  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的相关代码如下:

[cpp] view plaincopy
  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);

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

[cpp] view plaincopy
  1. class CC_DLL CCLayer : public CCNode, public CCTouchDelegate, public CCAccelerometerDelegate, public CCKeypadDelegate  


监听home键:

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

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

[cpp] view plaincopy
  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,我们就能在这两个方法做文章了。

相关代码如下:

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


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


0 0
原创粉丝点击