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