自从cocos2d-x2.0版本后,添加了一个CCControlButton,这个优先级为0,比默认的button高,因此,我们可以在当前层添加CCControlButton,在弹出的层设置layer的优先级比0小,这样,在弹出的层添加menu就能够响应,也不会影响后面的事件。当然,由于CCControlButton的优先级是可以自动设置的,所以,我们也可以设置弹出的层为-128(或者更小),然后设置CCControlButton的优先级更小就可以了。
具体代码如下:
BlockTouchLayer *block = BlockTouchLayer::create(); this->addChild(block);
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); CREATE_FUNC(BlockTouchLayer); }; #endif
BlockTouchLayer的具体实现文件:
#include "BlockTouchLayer.h" USING_NS_CC_EXT; bool BlockTouchLayer::init() { ccColor4B color = {0,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) { //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) { }
原文:
http://blog.csdn.net/somestill/article/details/12752739