
游戏暂停界面:
cocos2d-x中游戏暂停界面提供的思路是用pushScene()和popScne(),即推进和d出场景,当游戏暂停时,推进(pushScene())暂停场景,之前运行的场景将会自动暂停,然后我们可以在暂停场景中 *** 作,如Resume,ReStart,Quit等,当我们不再需要暂停场景时,可以popScene()将暂停场景d出。(场景就像一张纸,我们推进一个场景,相当于在这张纸上再盖上一张,d出场景相当于将最表面的那张纸拿掉)。
推进暂停场景的相关代码如下:
[cpp] view plain copy CCRenderTexture*renderTexture=CCRenderTexture::create(800,600); renderTexture->begin(); this->getParent()->visit(); renderTexture->end();//这里实际是通过CCRenderTexture保存当前界面(相当于截屏),然后传递给暂停界面,当成背景精灵 CCDirector::sharedDirector()->pushScene(PauseLayer::scene(renderTexture,true));暂停场景PauseLayer的相关代码如下: copy CCScene*PauseLayer::scene(CCRenderTexture*sqr,boolisFlip){ 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'isanautoreleaSEObject PauseLayer*layerr=PauseLayer::create(); //addlayerasachildtoscene m_scene->addChild(layerr); //returnthescene returnm_scene; }
监听返回键和Menu键:
要点:
1.继承CCKeypadDelegate
2.实现两个虚函数
| virtual voID | keyBackClicked() |
| virtual voID | keyMenuClicked() |
如查要实现监听的对象是cclayer或者继承cclayer的,则只需做第二步及在初始化中setKeypadEnabled(true);
因为cclayer本身继承了CCKeypadDelegate,如下图所示
copy classCC_DLLcclayer:publicCCNode,publicCCtouchDelegate,153); Font-weight:bold; background-color:inherit">publicCCAccelerometerDelegate,153); Font-weight:bold; background-color:inherit">publicCCKeypadDelegate监听home键:
在cocos2d-x中我现在还没找到明确的监听home键的方案,但可以用替代方案。
不知你们有没有发现在AppDelegate.cpp里的两个方法:
copy //ThisfunctionwillbecalleDWhentheappisinactive.Whencomesaphonecall,it'sbeinvokedtoo voIDAppDelegate::applicationDIDEnterBackground(){ CCDirector::sharedDirector()->stopAnimation(); //ifyouuseSimpleAudioEngine,itmustbepause //SimpleAudioEngine::sharedEngine()->pauseBackgroundMusic(); } //thisfunctionwillbecalleDWhentheappisactiveagain voIDAppDelegate::applicationWillEnterForeground(){ CCDirector::sharedDirector()->startAnimation(); //SimpleAudioEngine::sharedEngine()->resumeBackgroundMusic(); }注意这两个方法的英文解释,实际上这两个方法就是判断程序是否被切换或者说是否被扔至后台工作。因为在手机上按home键,实际就是切换将程序推至后台。So,我们就能在这两个方法做文章了。
相关代码如下:
copy voIDAppDelegate::applicationDIDEnterBackground() { SimpleAudioEngine::sharedEngine()->pauseBackgroundMusic(); Global*sh=Global::toIns(); CCRenderTexture*renderTexture; switch(sh->targetScene){ caseTargetSceneFirstScene: break; caseTargetScenesecondScene: renderTexture=CCRenderTexture::create(800,600); renderTexture->begin(); sh->battleLayer->visit(); renderTexture->end(); CCDirector::sharedDirector()->pushScene(PauseLayer::scene(renderTexture,153); Font-weight:bold; background-color:inherit">false)); caseTargetSceneInvalID: default: } //thisfunctionwillbecalleDWhentheappisactiveagain voIDAppDelegate::applicationWillEnterForeground() SimpleAudioEngine::sharedEngine()->resumeBackgroundMusic(); }在上面的代码中,我做的是,当程序InActive(推至后台)时,推进暂停界面
如果还有其它的解决方案,欢迎留言
总结以上是内存溢出为你收集整理的cocos2d-x 游戏暂停界面,监听home键,返回键,Menu键 解决方案全部内容,希望文章能够帮你解决cocos2d-x 游戏暂停界面,监听home键,返回键,Menu键 解决方案所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)