In the game, we often encounter some pop-up window, these pop-up window to prohibit the point through, that is, prohibit the touch event to the bottom, we call the occlusion layer, these occlusion layer, need to develop the occlusion layer, we must first understand the COCOS2D-JS touch transfer mechanism, this article mainly for Cocos2d-js v3.0 Final version.
According to the official documentation, we know that there are five ways to touch, but according to the requirements, we need to do is to intercept the touch monitoring.
So we simply encapsulate such a class as follows:
Acl Modellayercolor =cc. Layercolor.extend ({m_touchlistener:NULL, ctor:function(){ This. _super (); varTouchlistener ={event:cc. Eventlistener.touch_one_by_one, Swallowtouches:true, Ontouchbegan: This. Ontouchbegan}; Cc.eventManager.addListener (Touchlistener, This); This. M_touchlistener =Touchlistener; }, Ontouchbegan:function(Touch, event) {vartarget =Event.getcurrenttarget (); if(!target.isvisible () | | (! This. Istouchinside (Target,touch))) { return false; } return true; }, Istouchinside:function(Owner,touch) {if(!owner | |!owner.getparent ()) { return false; } varTouchlocation = Touch.getlocation ();//Get the touch positionTouchlocation =owner.getparent (). Converttonodespace (touchlocation); returnCc.rectcontainspoint (Owner.getboundingbox (), touchlocation); }});
This is to set the swallowtouches to true so that Ontouchbegan returns true to devour the touch without continuing to pass to the lower priority layer to implement the occlusion layer.
COCOS2D-JS Tutorial Cocos2d-js occlusion Layer (disable Touch Event pass layer)