cocos2d-x 游戏暂停和恢复,还可以屏蔽下层按钮触发
2014-05-29 15:40
411 查看
废话不多说(其实不知道说什么,呵呵),直接上代码。
此例是在游戏内点击暂停按钮源码
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);
}
此例是在游戏内点击暂停按钮源码
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);
}
相关文章推荐
- Cocos2d-x 中游戏暂停与恢复
- Cocos3.4 横版游戏制作-《KillBear》-暂停层+屏蔽下层监听
- cocos2d-x3.x屏蔽下层触摸(制作透明图层暂停界面)
- cocos2d-x-2.1.3 中实现游戏的简单暂停与恢复
- cocos2d-x3.x屏蔽下层触摸(制作透明图层暂停界面)
- cocos2d-x3.x屏蔽下层触摸(制作透明图层暂停界面)
- 怎么使游戏按下home的时候进入暂停状态 然后再次进入点击按钮恢复?
- cocos2d-x3.x屏蔽下层触摸(制作透明图层暂停界面)
- cocos2d-x3.x屏蔽下层触摸(制作透明图层暂停界面)
- cocos2d-x3.x屏蔽下层触摸(制作透明图层暂停界面)
- cocos2d-x3.x屏蔽下层触摸(制作透明图层暂停界面)
- cocos2d-x游戏暂停和恢复
- cocos2d-x3.x屏蔽下层触摸(制作透明图层暂停界面)
- cocos2d-x3.x屏蔽下层触摸(制作透明图层暂停界面)
- cocos2d-x3.x屏蔽下层触摸(制作透明图层暂停界面)
- cocos2d-x3.x屏蔽下层触摸(制作透明图层暂停界面)
- cocos2d-x3.x屏蔽下层触摸(制作透明图层暂停界面)
- cocos2d-x3.x屏蔽下层触摸(制作透明图层暂停界面)
- cocos2d-x3.x屏蔽下层触摸(制作透明图层暂停界面)
- 【cocos2d-x入门实战】微信飞机大战之十一:游戏暂停和触摸屏蔽