Mask Sprites with Clippingnode

Source: Internet
Author: User
Tags addchild

In the production of a elimination game, there is a situation: blocks from the top of the drop down, before entering the layout, is not displayed, otherwise affect the visual experience. So, since this block has already been added to the render tree, how can you make it appear in one part and display it in another?

This is going to cover the mask. In Cocos2d-x, a clipping node clippingnode is provided to achieve a clipping effect, so the clipped part is equal to the mask. It may have been more effective, and I have not studied it very well, just used it to deal with the above problems. The official introduction to this category is:

Http://www.cocos2d-x.org/docs/manual/framework/native/v3/ClippingNode/zh

The following records the method of using Clippingnode to complete the mask feature.

1. Create a new Clippingnode, which inherits from node;

2. To set the crop template for Clippingnode, the clipping template is also a node, but it is not displayed, and its default location is the coordinates origin of the Clippingnode;

3. Add child nodes for Clippingnode.

Clippingnode's child nodes are displayed only within the range of the clipping template, otherwise they are clipped or masked. It can be understood that there is a large black cloth, the contents of the Clippingnode inside all covered up, at this time, to the big black cloth opened a shape of the hole, as long as the inside of the hole in the content, will be displayed. This hole is the template.

The specific code is as follows:

    //Window SizeAuto winsize = Director::getinstance ()getwinsize (); //Create a cropping nodeAuto Clipper =clippingnode::create (); Clipper->setposition (Winsize.width/2, Winsize.height/2);    AddChild (clipper); //set a crop template that will not be drawn, just to mask theAuto stencil = Sprite::create ("Helloworld.png"); Clipper-Setstencil (stencil); //Add a crop node that does not appear in the Crop template rangeAuto Test_sprite = Sprite::create ("Powered.png"); Test_sprite->setposition ( -, -); Clipper->addchild (Test_sprite);

Display effect:

can see:

1. The crop template is not displayed, and the red box here is drawn separately to indicate the extent of the clipping template.

The child nodes of 2.ClippingNode are not rendered completely, only content that is inside the clip template will be rendered.

Mask Sprites with Clippingnode

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.