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

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);

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


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