cocos2dx屏蔽层的制作,防止当前层点击上一层。

前端之家收集整理的这篇文章主要介绍了cocos2dx屏蔽层的制作,防止当前层点击上一层。前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
#ifndef _ShieldLayer_H_
#define _ShieldLayer_H_

#include "cocos2d.h"
USING_NS_CC;

//////////////////////////////////////////////////////////////////////////
//屏蔽层
class ShieldLayer: public Layer
{
public:
	CREATE_FUNC(ShieldLayer);
	
private:
	ShieldLayer() {}
	~ShieldLayer() {
		mListener->release();
	}

	bool init()
	{
		do 
		{
			CC_BREAK_IF(!Layer::init());

			mListener = EventListenerTouchOneByOne::create();//创建一个触摸监听
			mListener->onTouchBegan = [](Touch* touch,Event* event) {
				log("ShieldLayer");
				return true; 
			};
			//mListener->onTouchMoved = [](Touch* touch,Event* event){};
			//mListener->onTouchEnded = [](Touch* touch,Event* event){};
			mListener->setSwallowTouches(true);
			mListener->retain();
			setTouchEnabled(true);
			return true;
		} while (0);
		return false;
	}

public:
	void setTouchEnabled(bool value)
	{
		if (value) {
			_eventDispatcher->removeEventListener(mListener);
			_eventDispatcher->addEventListenerWithSceneGraPHPriority(mListener,this);
		}
		else 
		{
			_eventDispatcher->removeEventListener(mListener);
		}
	}
private:
	EventListenerTouchOneByOne* mListener;
};

#endif // _ShieldLayer_H_
 
 

只要在当前层上创建该屏蔽层,就可以防止点击到上一层页面

猜你在找的Cocos2d-x相关文章