[Cocos2dx tips] Implementation of translucent shield and pop-up box, cocos2dx translucent

Source: Internet
Author: User

[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 endaddEventListenerWithSceneGraphPriorityYou 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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.