cocos2d-x添加触摸层阻止后端事件是怎样的,下面我们来看:
由于cocos2d-x中的优先级小的,先响应触摸事件,这是,我们只需要设置新添加的cclayer层的优先级即可,但由于ccmenu的优先级较高,所以,很有可能透过我们添加的触摸层,响应之前的绑定的button事件,而如果我们设置此触摸层优先级比button的低时,又导致在这层添加的button按钮的触摸事件不能触发,导致本层应该触摸的事件也不能响应,这样也达不到我们理想的效果:弹出一个层,屏蔽后面的事件,在当层可以有一些按钮,让我们进行不同的选择,比如确定,or 取消。
自从cocos2d-x2.0版本后,添加了一个CCControlButton,这个优先级为0,比默认的button高,因此,我们可以在当前层添加CCControlButton,在弹出的层设置layer的优先级比0小,这样,在弹出的层添加menu就能够响应,也不会影响后面的事件。当然,由于CCControlButton的优先级是可以自动设置的,所以,我们也可以设置弹出的层为-128(或者更小),然后设置CCControlButton的优先级更小就可以了。
具体代码如下:[cpp]
BlockTouchLayer *block = BlockTouchLayer::create();
this->addChild(block);
BlockTouchLayer的声明文件:
[cpp] view plaincopy在CODE上查看代码片派生到我的代码片
#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
BlockTouchLayer的具体实现文件:[cpp]
#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)
{
}
最终实现页面: