cocos2d-x新增觸控層阻止後端事件
阿新 • • 發佈:2019-02-01
由於cocos2d-x中的優先順序小的,先響應觸控事件,這是,我們只需要設定新新增的cclayer層的優先順序即可,但由於ccmenu的優先順序較高,所以,很有可能透過我們新增的觸控層,響應之前的繫結的button事件,而如果我們設定此觸控層優先順序比button的低時,又導致在這層新增的button按鈕的觸控事件不能觸發,導致本層應該觸控的事件也不能響應,這樣也達不到我們理想的效果:彈出一個層,遮蔽後面的事件,在當層可以有一些按鈕,讓我們進行不同的選擇,比如確定,or 取消。
自從cocos2d-x2.0版本後,添加了一個CCControlButton,這個優先順序為0,比預設的button高,因此,我們可以在當前層新增CCControlButton,在彈出的層設定layer的優先順序比0小,這樣,在彈出的層新增menu就能夠響應,也不會影響後面的事件。當然,由於CCControlButton的優先順序是可以自動設定的,所以,我們也可以設定彈出的層為-128(或者更小),然後設定CCControlButton的優先順序更小就可以了。
具體程式碼如下:
BlockTouchLayer *block = BlockTouchLayer::create();
this->addChild(block);
BlockTouchLayer的宣告檔案:
BlockTouchLayer的具體實現檔案:#ifndef __test__BlockTouchLayer__ #define __test__BlockTouchLayer__ #include <iostream> #include "cocos2d.h" #include "cocos-ext.h" using namespace cocos2d; class BlockTouchLayer :public CCLayerColor { public: virtual bool init(); void initData(); 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); void callBack(CCObject *pSender,CCControlEvent event); CREATE_FUNC(BlockTouchLayer); }; #endif
最終實現頁面:#include "BlockTouchLayer.h" USING_NS_CC_EXT; bool BlockTouchLayer::init() { ccColor4B color = {100,0,0,125}; //設定面板顏色及透明度 if (!CCLayerColor::initWithColor(color)) { return false; } initData(); setTouchEnabled(true); //開啟觸控事件 return true; } void BlockTouchLayer::initData() { CCSize winsize = CCDirector::sharedDirector()->getWinSize(); CCSprite *sp = CCSprite::create("Icon.png"); this->addChild(sp); sp->setPosition(ccp(winsize.width/2,winsize.height/2)); CCSpriteFrameCache *cache = CCSpriteFrameCache::sharedSpriteFrameCache(); cache->addSpriteFramesWithFile("load.plist", "load.png"); CCSprite *sprite1 = CCSprite::createWithSpriteFrameName("loading1.png"); sprite1->setPosition(ccp(winsize.width/2, winsize.height/2)); //this->addChild(sprite1); CCSpriteBatchNode *batchNode = CCSpriteBatchNode::create("load.png"); batchNode->addChild(sprite1); this->addChild(batchNode); // 可以起到一定的優化作用,最好不要直接新增精靈 CCArray *spriteArray = CCArray::create(); char str[100] = {0}; for (int i=1; i <= 14; i++) { sprintf(str, "loading%d.png", i); CCSpriteFrame *frame = cache->spriteFrameByName(str); spriteArray->addObject(frame); } CCAnimation *animation = CCAnimation::createWithSpriteFrames(spriteArray,0.5); sprite1->runAction(CCRepeatForever::create(CCAnimate::create(animation))); cocos2d::extension::CCScale9Sprite *sprite9 = cocos2d::extension::CCScale9Sprite::create("btn_blackblue.png"); CCLabelTTF *label = CCLabelTTF::create("click me", "", 22); cocos2d::extension::CCControlButton *controlButton = cocos2d::extension::CCControlButton::create(label, sprite9); controlButton->addTargetWithActionForControlEvents(this, cccontrol_selector(BlockTouchLayer::callBack), cocos2d::extension::CCControlEventTouchDown); this->addChild(controlButton); controlButton->setPosition(ccp(winsize.width/3, winsize.height/2)); controlButton->setTouchPriority(-200); //設定CCControlButton的優先順序 //controlButton->setZoomOnTouchDown(true); } void BlockTouchLayer::callBack(cocos2d::CCObject *pSender,CCControlEvent event) { //CCNotificationCenter::sharedNotificationCenter()->postNotification("click",this); BlockTouchLayer *tmplayer = (BlockTouchLayer *)((CCMenu *)(pSender))->getParent(); tmplayer->removeFromParentAndCleanup(true); //點選按鈕後,移除彈出的面板 } void BlockTouchLayer::registerWithTouchDispatcher() { CCDirector::sharedDirector()->getTouchDispatcher()->addTargetedDelegate(this, -128, true); //註冊並設定當前面板觸控的優先順序 } bool BlockTouchLayer::ccTouchBegan(cocos2d::CCTouch *pTouch, cocos2d::CCEvent *pEvent) { return true; // 吞噬掉後面的響應 } void BlockTouchLayer::ccTouchEnded(cocos2d::CCTouch *pTouch, cocos2d::CCEvent *pEvent) { } void BlockTouchLayer::ccTouchMoved(cocos2d::CCTouch *pTouch, cocos2d::CCEvent *pEvent) { }
第一張圖片點選右下角的退出按鈕,沒有反應,點選click me按鈕,取消當前層顯示原來的hello world層,此時退出按鈕有效。