原創作品,轉載請標明: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())
(暫停狀態)