【cocos2d-x入門實戰】微信飛機大戰之十一:遊戲暫停和觸控螢幕蔽

來源:互聯網
上載者:User

原創作品,轉載請標明:http://blog.csdn.net/jackystudio/article/details/11999229

一個遊戲打到一半尿點咋整?難道要憋著。。。這不科學啊!

好吧,把暫停遊戲和恢複遊戲的功能加進去吧,否則也太對不起觀眾了!

//加入暫停按鈕bool ControlLayer::init(){bool bRet=false;do {CC_BREAK_IF(!CCLayer::init());CCSize winSize=CCDirector::sharedDirector()->getWinSize();//加入PauseMenuCCSprite* normalPause=CCSprite::create(CCSpriteFrameCache::sharedSpriteFrameCache()->spriteFrameByName("game_pause_nor.png"));CCSprite* pressedPause=CCSprite::create(CCSpriteFrameCache::sharedSpriteFrameCache()->spriteFrameByName("game_pause_pressed.png"));pPauseItem=CCMenuItemImage::create();//建立CCMenuItempPauseItem->initWithNormalSprite(normalPause,pressedPause,NULL,this,menu_selector(ControlLayer::menuPauseCallback));//載入雙態圖和回呼函數pPauseItem->setPosition(ccp(normalPause->getContentSize().width/2+10,winSize.height-normalPause->getContentSize().height/2-10));CCMenu *menuPause=CCMenu::create(pPauseItem,NULL);//建立CCMenu,可以這麼理解CCMenuItem是CCMenu的孩子menuPause->setPosition(CCPointZero);this->addChild(menuPause,101);bRet=true;} while (0);return bRet;}
//暫停按鍵的回呼函數void ControlLayer::menuPauseCallback(CCObject* pSender){if(!CCDirector::sharedDirector()->isPaused())//如果遊戲處於正常狀態{//更改為恢複按鈕的雙態pPauseItem->setNormalSpriteFrame(CCSpriteFrameCache::sharedSpriteFrameCache()->spriteFrameByName("game_resume_nor.png"));pPauseItem->setSelectedSpriteFrame(CCSpriteFrameCache::sharedSpriteFrameCache()->spriteFrameByName("game_resume_pressed.png"));CCDirector::sharedDirector()->pause();//暫停遊戲,這是導演控制的}else//否則{//更改為暫停按鈕的雙態pPauseItem->setNormalSpriteFrame(CCSpriteFrameCache::sharedSpriteFrameCache()->spriteFrameByName("game_pause_nor.png"));pPauseItem->setSelectedSpriteFrame(CCSpriteFrameCache::sharedSpriteFrameCache()->spriteFrameByName("game_pause_pressed.png"));CCDirector::sharedDirector()->resume();//開麥拉!}}

。關於觸摸事件和優先順序,請移步:http://blog.csdn.net/jackystudio/article/details/11860007,再次強調,觸摸優先順序和addChild的Z軸順序無關。

//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);//設定觸摸有效return true;  }  void NoTouchLayer::registerWithTouchDispatcher(){  CCDirector::sharedDirector()->getTouchDispatcher()->addTargetedDelegate(this, -127 , true);//優先順序低於-128(CCMenu),同時高於其他層(0),true表示吞噬觸摸CCLayer::registerWithTouchDispatcher();  }  bool NoTouchLayer::ccTouchBegan (CCTouch *pTouch, CCEvent *pEvent){     return true;//返回true接收觸摸}  void NoTouchLayer::ccTouchMoved (CCTouch *pTouch, CCEvent *pEvent){  }  void NoTouchLayer::ccTouchEnded (CCTouch *pTouch, CCEvent *pEvent){  }  

if(isAlive && !CCDirector::sharedDirector()->isPaused())

(暫停狀態)


相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.