[Cocos2dx tips] Implementation of translucent shield and pop-up box, cocos2dx translucent
Today, we will introduce the pop-up box and the small Implementation of the Screen Shield ~
The pop-up box mainly uses the OnEnter () function in the cocos2dx lifecycle, it is the function that will be called when the Layer is addChild (so if you add the OnEnter code to init, the effect will be the same ~)
The Screen Shield uses MenuItemImage and LayerColor. The former can set the area size and obtain the click, and the latter can set Color4B. As follows:
Main Code:
// ShaderLayer. hvoid setClickBgBlock (std: function <void ()> block); std: function <void ()> clickBGBlock; // ShaderLayer. cppauto item = MenuItemImage: create (); item-> setContentSize (visibleSize); item-> setCallback ([&] (Ref * pSender) {// execute if (clickBGBlock! = NULL) {this-> clickBGBlock ();}});
If you are not familiar with block in ios, search for lambda (I think it is an anonymous function pointer ). Then, when the MenuItemImage is clicked, The clickBGBlock () function is called;
// HelloWorldScene. cpp // mask the Touch auto listener = EventListenerTouchOneByOne: create (); listener-> setSwallowTouches (true); listener-> onTouchBegan = [] (touch * Touch, event * event) {auto target = static_cast <Sprite *> (event-> getCurrentTarget (); Vec2 locationInNode = target-> convertToNodeSpace (touch-> getLocation ()); size s = target-> getContentSize (); Rect rect = Rect (0, 0, s. width, s. height); if (rect. containsPoint (locationInNode) {// return true swallowTouches to return true;} return false ;};
This piece of code is generic, as long as you add a sentence at the endaddEventListenerWithSceneGraphPriority
You can listen to the touch control (within the ContentSize of the target you listen ).
(I always think there should be a simpler way to implement this function. If you know, please let me know immediately ~)
Attachment code + picture: http://7sblre.com1.z0.glb.clouddn.com/PopUpAndShaderLayer.zip
Welcome to my blog: helkyle. tk