您的位置:首页 > 移动开发 > Cocos引擎

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

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: