Implementation of "COCOS2DX tips" translucent shielding hood and popup box

Source: Internet
Author: User

Introduce today, the popup box and the small implementation of the shielding cover ~
The popup box mainly uses the OnEnter () function in the COCOS2DX life cycle, which is the function that will be called when the layer is addchild (so if the OnEnter code is added to the INIT, the effect should be the same)
The shield is implemented using the Menuitemimage and Layercolor, the former can set the area size and get the click, the latter can set the 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){    //背景图点击时候执行    if (clickBGBlock != NULL) {        this -> clickBGBlock();    }});

If you are unfamiliar with blocks in iOS, search for lambda (I think it's an anonymous function pointer). Then when the menuitemimage is clicked, the Clickbgblock () function is called;

//helloworldscene.cpp//screen on the popup touch Auto listener = Eventlistenertouchonebyone:: Create (); listener-setswallowtouches (true), listener-Ontouchbegan = [] (Touch *touch, Event *event) {Auto Targ    ET = 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)) {//Here return True swallowtouches only valid return true; } return false;};  

This piece of code, is generic, as long as the last side to add a sentence addEventListenerWithSceneGraphPriority to listen to the touch control, (you listen to the target of the contentsize inside).

(I always think there should be a simpler way to do this, if you know, please tell me the first time ~)
Attached code + Picture: Http://7sblre.com1.z0.glb.clouddn.com/PopUpAndShaderLayer.zip

Welcome to my blog:helkyle.tk

Implementation of "COCOS2DX tips" translucent shielding hood and popup box

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.