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_


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

你可能感兴趣的:(C++,cocos2d-x)