[Cocos2d-x entry practice] WeChat aircraft war eleven: Game pause and touch Shield

Source: Internet
Author: User

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)


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.