防止touch事件穿层从而触发下层menu响应

原创作品,允许转载,转载时请务必以超链接形式标明文章 原始出处 、作者信息和本声明。否则将追究法律责任。

 http://blog.csdn.net/azhou_hui/article/details/8204445

 

有时我们游戏需要弹出框,当我们点击弹出框,会触发下面的menu,导致游戏错乱,出现我们非预想的结果。出现这种情况是由于CCMenu触摸事件响应优先级最高为-128,

那么我们为了达到我们理想的结果,就得设置当前layer的触摸响应优先级为<=-128。

 

(1)ThisLayer.h

class ThisLayer : public CCLayer
{
public:
	// init
	virtual bool init();

	CREATE_FUNC(CityBuildingInfoLayer);

	// the layer touch
	virtual void registerWithTouchDispatcher();
	virtual bool ccTouchBegan(CCTouch* pTouch, CCEvent* pEvent);
	virtual void ccTouchMoved(CCTouch* pTouch, CCEvent* pEvent);
	virtual void ccTouchEnded(CCTouch* pTouch, CCEvent* pEvent);

};


(2)ThisLayer.cpp

// on "init" you need to initialize your instance
bool ThisLayer::init()
{
	bool bRet = false;
	do 
	{
		CC_BREAK_IF(! CCLayer::init());

		// 开启点击
		setTouchEnabled(true);

		bRet = true;
	} while (0);

	return bRet;
}
// the layer touch
void ThisLayer::registerWithTouchDispatcher()
{
	//使用-128和CCMenu优先级相同,并且吞掉事件true//
	CCDirector::sharedDirector()->getTouchDispatcher()->addTargetedDelegate(this, INT_MIN+1, true);
	CCDirector::sharedDirector()->getTouchDispatcher()->addTargetedDelegate(this, -128, true);
	CCLayer::registerWithTouchDispatcher();
}
bool ThisLayer::ccTouchBegan(CCTouch* pTouch, CCEvent* pEvent)
{
	return true;
}
void ThisLayer::ccTouchMoved(CCTouch* pTouch, CCEvent* pEvent){}
void ThisLayer::ccTouchEnded(CCTouch* pTouch, CCEvent* pEvent){}



 

你可能感兴趣的:(防止touch事件穿层从而触发下层menu响应)