cocos2d-x添加触摸层阻止后端事件
由于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的声明文件:
#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
相关推荐
Cricket 2020-05-31
woxmh 2019-12-30
CaptainCTR 2019-12-19
86463960 2013-07-15
80487916 2013-07-06
86463960 2013-08-28
BenOnceMore 2013-08-26
89741733 2014-03-20
86201242 2014-05-13
85427010 2014-09-04
文艺小青年 2015-03-26
cooclc 2015-03-26
85427010 2015-05-22
frankwang 2015-05-19
80487916 2015-05-18
进入场景而且过渡动画结束时候触发。提示 GameScene场景中的继承于节点,这些生命周期事件根本上是从Node继承而来。事实上所有Node对象都有这些事件,具体实现代码与GameScene场景类似。
87921036 2015-05-12
85427010 2011-12-31
yizhiyanstart 2011-12-31