cocos2d-x 游戏暂停和恢复,还可以屏蔽下层按钮触发

废话不多说(其实不知道说什么,呵呵),直接上代码。

此例是在游戏内点击暂停按钮源码

h文件

#ifndef __MGGPAUSELAYER_H__
#define __MGGPAUSELAYER_H__

#include "cocos2d.h"
#include "MGGGameScene.h"

USING_NS_CC;

//优先级
#define HANDLERPRIORITY (kCCMenuHandlerPriority - 1)

class MGGPauseLayer : public CCLayer
{
public:
static MGGPauseLayer* createMGGPauseLayer(MGGGameScene* game);

void initMGGPauseLayer(MGGGameScene* game);

void menuContinueCallback(CCObject* pSender);
void menuHelpCallback(CCObject* pSender);

//暂停游戏
void pauseAllChildren(CCNode* node);
//恢复游戏
void resumeAllChildren(CCNode* node);

virtual void registerWithTouchDispatcher();

virtual bool ccTouchBegan(CCTouch* pTouch, CCEvent* pEvent) {return true;}
virtual void ccTouchMoved(CCTouch* pTouch, CCEvent* pEvent) {}
virtual void ccTouchEnded(CCTouch* pTouch, CCEvent* pEvent) {}

CC_SYNTHESIZE(MGGGameScene*, theGame, TheGame);
};

#endif


c文件

#include "MGGPauseLayer.h"
#include "MGGWelcomeScene.h"

MGGPauseLayer* MGGPauseLayer::createMGGPauseLayer(MGGGameScene* game)
{
MGGPauseLayer* pMGGPauseLayer = new MGGPauseLayer();

if (pMGGPauseLayer && pMGGPauseLayer->init())
{
pMGGPauseLayer->autorelease();
pMGGPauseLayer->initMGGPauseLayer(game);
return pMGGPauseLayer;
}
CC_SAFE_DELETE(pMGGPauseLayer);
return NULL;
}

void MGGPauseLayer::initMGGPauseLayer(MGGGameScene* game)
{
CCSize visibleSize = CCDirector::sharedDirector()->getVisibleSize();

theGame = game;

CCLayerColor* pLayerColor = CCLayerColor::create(ccc4(0, 0, 0, 200));
this->addChild(pLayerColor);

//添加继续游戏
CCMenuItemImage* pContinueMenu = CCMenuItemImage::create("pause/continue1.png", "pause/continue2.png", this, menu_selector(MGGPauseLayer::menuContinueCallback));
pContinueMenu->setPosition(ccp(visibleSize.width * 0.5, visibleSize.height * 0.5 + pContinueMenu->getContentSize().height));

//添加游戏帮助按钮
CCMenuItemImage* pHelpMenu = CCMenuItemImage::create("pause/help1.png", "pause/help2.png", this, menu_selector(MGGPauseLayer::menuHelpCallback));
pHelpMenu->setPosition(ccp(visibleSize.width * 0.5, visibleSize.height * 0.5 - pHelpMenu ->getContentSize().height));

CCMenu* pMenu = CCMenu::create(pContinueMenu, pHelpMenu, NULL);
pMenu->setPosition(CCPointZero);
pMenu->setTouchPriority(HANDLERPRIORITY);
this->addChild(pMenu);

//暂停游戏
pauseAllChildren(theGame);

this->setTouchEnabled(true);
}

void MGGPauseLayer::menuContinueCallback(CCObject* pSender)
{
//恢复游戏
resumeAllChildren(theGame);
this->removeFromParentAndCleanup(true);
}

void MGGPauseLayer::menuHelpCallback(CCObject* pSender)
{

}

//暂停游戏
void MGGPauseLayer::pauseAllChildren(CCNode* node)
{  
node->pauseSchedulerAndActions();

CCObject * obj; 
CCARRAY_FOREACH(node->getChildren(), obj)
{
CCNode* n = (CCNode*)obj;
pauseAllChildren(n);
}
}

//恢复游戏
void MGGPauseLayer::resumeAllChildren(CCNode* node)
{
node->resumeSchedulerAndActions();

CCObject* obj;
CCARRAY_FOREACH(node->getChildren(), obj)
{  
CCNode* n = (CCNode*)obj;
resumeAllChildren(n);
}
}

void MGGPauseLayer::registerWithTouchDispatcher()
{
CCDirector::sharedDirector()->getTouchDispatcher()->addTargetedDelegate(this, HANDLERPRIORITY, true);
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值