Clippingnode Introduction First, a simple look at the use of the Clippingnode class. As the name implies, first it is a node that can be used as a container for other sprite,node, and a node that can be clipped. How to crop, define a set of rules for cropping out. Here you can use a picture to crop according to the resolution of the image or an effective pixel, or draw a clipping area yourself to crop it according to the area. The picture or the area of the drawing, is the template, Clippingnode according to the template to cut the picture. Main methods:
Static clippingnode* Create (Node *stencil); // The create function passes in a template that can be a sprite or a drawnode (custom graphic) const; void setalphathreshold (glfloat alphathreshold); This method is more important. Setting the alpha value, which is related to the transparency of the image, is 1 by default, which means that all pixels in the image are displayed. Includes transparent areas. You generally want to not show transparent areas, set to 0. to . Here's how to crop a picture, you can also use a circular picture, the middle cutout. Then you need to set Setalphathreshold, if not set, the cropped image is square, is the actual size of the picture.
BOOL Const ; void setinverted (bool inverted); // shows the cropped part, or the part that is cropped
Circularnode circular Picture Class There are two ways to write this class. One is, let the art to cut a round picture, the Middle hollow, with this circular picture as Clippingnode template to cut, but must be set Setalphathreshold (0.05). Another way is that the code below, it is not troublesome art, can save a few KB to save a few kilobytes. Let's draw a circle ourselves. Just write an interface, you need to expand, follow the comments to see it. Header file
#ifndef __circularnode__#define__circularnode__#include<stdio.h>#include"cocos2d.h"#include"extensions/cocos-ext.h"classCircularnode: Publiccocos2d::clippingnode{ Public: Circularnode (); Virtual~Circularnode (); /** * Create a circular clippingnode * * @param radius created circle radius * * @return return a cut node*/ Staticcircularnode* Create (floatradius); /** * Create a circular clippingnode * * @param radius created circle radius * @param sprite needs to be cut round the sprite * * @return back A cut node*/ Staticcircularnode* Create (floatRadius, cocos2d::node*Pnode); Virtual BOOLInitfloatradius); Cc_property (Cocos2d::node*, M_clipnode, Clipnode);};#endif
Specific implementation
#include"CirCularNode.h"using_ns_cc; Circularnode::circularnode (): M_clipnode (nullptr) {}circularnode::~Circularnode () {cc_safe_release_null (m_clipnode);} Circularnode* Circularnode::create (floatradius) {Auto Pclipnode=NewCircularnode (); if(Pclipnode && pclipnode->init (RADIUS)) {Pclipnode-autorelease (); } Else { DeletePclipnode; Pclipnode=nullptr; } returnPclipnode;}BOOLCircularnode::init (floatradius) { if(!Clippingnode::init ()) {Cclog ("circularnode parent Init failed!"); return false; } //draw a circle with DrawnodeAuto Circlenode =drawnode::create (); //The number of vertex coordinates, when the need to draw a large circle, the value will be correspondingly bigger a little Const intMaxtrangle = the; //Vertex ArrayVEC2 Circlevec2[maxtrangle]; //calculates the coordinates of each point above the circle for(inti =0; i < Maxtrangle; i + +) { floatx = COSF (i * (m_pi/ the. f)) *radius; floaty = Sinf (i * (m_pi/ the. f)) *radius; Circlevec2[i]=Vec2 (x, y); } //ColorAuto Circlecolor = color4f (0,1,0,1); Circlenode->drawpolygon (CIRCLEVEC2, Maxtrangle, Circlecolor,1, Circlecolor); //set the template class for ClippingnodeSetstencil (Circlenode); return true;} Circularnode* Circularnode::create (floatRadius, node*Pnode) {Auto Clipnode=circularnode::create (RADIUS); if(clipnode) {Clipnode-Setclipnode (Pnode); } returnClipnode;}voidCircularnode::setclipnode (node*Pnode) {cc_safe_release_null (M_clipnode); M_clipnode=Pnode; Cc_safe_retain (M_clipnode); AddChild (Pnode);} Node*Circularnode::getclipnode () {returnM_clipnode;}
COCOS2DX 3.X using Clippingnode to cut a picture into a circle