Disable back layers touch while pop up is thrown

Hi,

i want to show a pop up of game exit on keybackclicked() event, the issue is pop up is thrown but back layer still takes the touch

i tried using this~~>setTouchEnabled; but failed, can anyone help me with this ?
Below is the code
void MenuLayer::keyBackClicked
{
this~~>setTouchEnabled(false);

mainSceneRef~~>commonLayerRef = new CommonLayer;
mainSceneRef~~>addChild(mainSceneRef~~>commonLayerRef);
mainSceneRef~~>commonLayerRef~~>keyBackClicked;
}
void MenuLayer::Quit
{
CCDirector::sharedDirector~~>end;
}
void MenuLayer::UnQuit
{
this~~>removeChild;
this~~>removeChild(qButtons, true);
CCDirector::sharedDirector()->resume();

CCDirector::sharedDirector()->startAnimation();

CocosDenshion::SimpleAudioEngine::sharedEngine()->resumeBackgroundMusic();

}

Make sure the top layer has highest priority (priority for touch handling is the highest negative value gets touches first), and swallows touches. If the priority of touches is correct you shouldn’t need to do any extra work.

Hi,

How do we get the touch handling priority for a layer?

Thanks in advance

@jonah13

The pop up layer has to swallow touches i.e.,
popUpLayer->setSwallowTouches(true);

and

The zOrder of popUpLayer has to be higher than the layer behind it.

This is a standard use of a cover layer. Here is my implementation:

    #ifndef __COVER_LAYER_H__
#define __COVER_LAYER_H__
/*
	The cover layer is basically a layer that stops touches from going to its parent.
	Right now this is hackish. If it doesn't work I can recursively turn off touches
	for all parents... (think about that...)

	The cover layer also adds a black tint to whatever it's covering to give it a
	"disabled" modal look.
*/

#include "cocos2d.h"
#include "ui/CocosGUI.h"

class CoverLayer : public cocos2d::LayerColor
{
public:
	// there's no 'id' in cpp, so we recommend returning the class instance pointer
	// Here's a difference. Method 'init' in cocos2d-x returns bool, instead of returning 'id' in cocos2d-iphone
	virtual bool init();
	virtual bool onTouch(cocos2d::Touch*, cocos2d::Event*);
	// implement the "static create()" method manually
	CREATE_FUNC(CoverLayer);
protected:
	bool _covered = true;
};

#endif // __COVER_LAYER_H__

The CPP file:

#include "cocos2d.h"
#include "Layers\Common\CoverLayer.h"
#include "ui/CocosGUI.h"

bool CoverLayer::init() {
	if (!cocos2d::LayerColor::initWithColor(cocos2d::Color4B(0, 0, 0, 0)))
	{
		return false;
	}

	this->setPosition(cocos2d::Vec2(0, 0));
	auto director = cocos2d::Director::getInstance();
	cocos2d::Size Size = director->getWinSize();
	this->setContentSize(Size);
	// this->setColor(cocos2d::ColorB(255, 255, 255,255));
	this->setOpacity(128); // Opacity 0 - 255

	auto touchListener = cocos2d::EventListenerTouchOneByOne::create();
	touchListener->setSwallowTouches(true);
	touchListener->onTouchBegan = CC_CALLBACK_2(CoverLayer::onTouch, this);
	director->getEventDispatcher()->addEventListenerWithSceneGraphPriority(touchListener, this);
	return true;
}

bool CoverLayer::onTouch(cocos2d::Touch* touch, cocos2d::Event* event)
{
	// cocos2d::log("touch swallowed");
	return true;
// Swallow them touches whole!
}
2 Likes

Yeah since I’ve posted that two years ago, touch handling has been greatly simplified. It’s more intuitive; things on top (z order) get touches first with default touch handling.

hi @Jgod

Could you share the link of your original post please ?
Thanks. :smile:

There wasn’t really a link to it but I’ve found someone describing how it was (2.x ish?) on stackoverflow. Maybe you could find the old docs for those versions.

Those were dark days: disabling layers and managing touch priorities (which were opposite of render order :mask:).