Original Work, reprinted, please mark: Http://blog.csdn.net/jackystudio/article/details/11999229
What if a game hits half of the urine? Are you holding your breath... This is not scientific!
Okay. Add the pause and resume functions to the game. Otherwise, I am too sorry for the audience!
// Add the pause button bool ControlLayer: init () {bool bRet = false; do {CC_BREAK_IF (! CCLayer: init (); CCSize winSize = CCDirector: shareddire()-> getWinSize (); // Add PauseMenuCCSprite * normalPause = CCSprite: create (CCSpriteFrameCache :: duration ()-> spriteFrameByName ("duration"); CCSprite * pressedPause = CCSprite: create (CCSpriteFrameCache: duration ()-> spriteFrameByName ("duration ")); pPauseItem = CCMenuItemImage: create (); // create metrics-> initWithNormalSprite (normalPause, pressedPause, NULL, this, menu_selector (ControlLayer: menuPauseCallback )); // load the two-state graph and the callback function pPauseItem-> setPosition (ccp (normalPause-> getContentSize (). width/2 + 10, winSize. height-normalPause-> getContentSize (). height/2-10); CCMenu * menuPause = CCMenu: create (pPauseItem, NULL); // create CCMenu, it can be understood that CCMenuItem is the Child menuPause of CCMenu-> setPosition (CCPointZero); this-> addChild (menuPause, 101); bRet = true;} while (0); return bRet ;}
// The callback function void ControlLayer: menuPauseCallback (CCObject * pSender) {if (! CCDirector: sharedDirector ()-> isPaused () // if the game is in normal state {// change to a recovery-type two-state pPauseItem-> setNormalSpriteFrame (CCSpriteFrameCache: Resume () -> outputs ("cached"); pPauseItem-> setSelectedSpriteFrame (CCSpriteFrameCache: cached ()-> spriteFrameByName ("cached"); CCDirector: shareddire () -> pause (); // pause the game. This is controlled by the Director} else // otherwise {// change it to the two-state pPauseItem-> s EtNormalSpriteFrame (CCSpriteFrameCache: cached ()-> cached ("cached"); pPauseItem-> setSelectedSpriteFrame (CCSpriteFrameCache: cached ()-> cached ("cached ")); CCDirector: sharedDirector ()-> resume (); // kaima !}}
. For more information about touch events and priorities, see the following steps: Configure.
// NoTouchLayer. hclass NoTouchLayer: public CCLayer {public: virtual bool init (); // implement the "static node ()" method manually LAYER_CREATE_FUNC (NoTouchLayer); virtual void registerWithTouchDispatcher (); virtual bool ccTouchBegan (cocos2d: CCTouch * pTouch, cocos2d: CCEvent * pEvent); virtual void ccTouchMoved (cocos2d: CCTouch * pTouch, cocos2d: CCEvent * pEvent ); virtual void ccTouchEnded (cocos2d :: CCTouch * pTouch, cocos2d: CCEvent * pEvent) ;}; // NoTouchLayer. cppbool NoTouchLayer: init () {if (! CCLayer: init () {return false;} setTouchEnabled (true); // set the valid return true;} void NoTouchLayer: registerWithTouchDispatcher () {CCDirector: shareddire () -> getTouchDispatcher ()-> addTargetedDelegate (this,-127, true); // The priority is lower than-128 (CCMenu) and higher than other layers (0 ), true indicates that the received touch is swallowed up by the CCLayer: registerWithTouchDispatcher ();} bool NoTouchLayer: ccTouchBegan (CCTouch * pTouch, CCEvent * pEvent) {return true; // return true:: ccTouchMoved (CCTouch * pTouch, CCEvent * pEvent) {} void NoTouchLayer: ccTouchEnded (CCTouch * pTouch, CCEvent * pEvent ){}
if(isAlive && !CCDirector::sharedDirector()->isPaused())
(Paused)