大佬教程收集整理的这篇文章主要介绍了cocos2dx 3.2 利用clippingNode把图片裁剪成圆形,接口可直接使用,大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
/** Creates and initializes a clipping node with an other node as its stencil. The stencil node will be retained. */ static ClippingNode* create(Node *stencil);create函数中传入一个模板,可以是一个sprite,也可以是一个drawNode(自定义的图形)。
/** The alpha threshold. The content is drawn only where the stencil have pixel with alpha greater than the alphaThreshold. Should be a float between 0 and 1. This default to 1 (so alpha test is disabled). */ GLfloat getAlphaThreshold() const; void setAlphaThreshold(GLfloat alphaThreshold);这个方法比较重要。设置alpha的值,跟图片的透明度有关,默认是1,就是图片中所有像素点都显示出来。包括透明区域。一般想不显示透明区域,则设置为0.05。
/** Inverted. If this is set to true,the stencil is inverted,so the content is drawn where the stencil is NOT drawn. This default to false. */ bool isInverted() const; void seTinverted(bool inverted);显示裁剪的部分,还是被裁剪的部分。
#ifndef __CirCularNode__ #define __CirCularNode__ #include <stdio.h> #include "cocos2d.h" #include "extensions/cocos-ext.h" class CirCularNode:public cocos2d::ClippingNode { public: CirCularNode(); virtual ~CirCularNode(); /** * 创建一个圆形clippingNode * * @param radius 创建的圆形半径 * * @return 返回一个剪切node */ static CirCularNode* create(float radius); /** * 创建一个圆形的clippingNode * * @param radius 创建的圆形半径 * @param sprite 需要切呈圆形的精灵 * * @return 返回一个剪切node */ static CirCularNode* create(float radius,cocos2d::Node* pNodE); virtual bool init(float radius); CC_PROPERTY(cocos2d::Node*,m_clipNode,ClipNodE); }; #endif实现:
#include "CirCularNode.h" USING_NS_Cc; CirCularNode::CirCularNode() :m_clipNode(nullptr) { } CirCularNode::~CirCularNode() { CC_SAFE_RELEASE_NULL(m_clipNodE); } CirCularNode* CirCularNode::create(float radius) { auto pClipNode = new CirCularNode(); if (pClipNode && pClipNode->init(radius)) { pClipNode->autorelease(); } else { delete pClipNode; pClipNode = nullptr; } return pClipNode; } bool CirCularNode::init(float radius) { if (!ClippingNode::init()) { CCLOG("CirCularNode parent init failed!"); return false; } //使用drawNode画圆形 auto circleNode = DrawNode::create(); //顶点坐标个数,在需要画大圆的时候,这个值就要相应变大一点 const int maxTrangle = 360; //顶点数组 Vec2 circleVeC2[R_663_11845@axTrangle]; //计算圆上面的各个点的坐标 for (int i = 0; i < maxTrangle; i ++) { float x = cosf( i * (M_PI/180.f)) * radius; float y = sinf( i * (M_PI/180.f)) * radius; circleVeC2[i] = Vec2(x,y); } //颜色 auto circleColor = Color4F(0,1,1); circleNode->drawPolygon(circleVec2,maxTrangle,circleColor,circleColor); //设置clippingNode的模板类 setStencil(circleNodE); return true; } CirCularNode* CirCularNode::create(float radius,Node* pNodE) { auto clipNode = CirCularNode::create(radius); if (clipNodE) { clipNode->setClipNode(pNodE); } return clipNode; } void CirCularNode::setClipNode(Node* pNodE) { CC_SAFE_RELEASE_NULL(m_clipNodE); m_clipNode = pNode; CC_SAFE_RETAIN(m_clipNodE); addChild(pNodE); } Node* CirCularNode::getClipNode() { return m_clipNode; }
以上是大佬教程为你收集整理的cocos2dx 3.2 利用clippingNode把图片裁剪成圆形,接口可直接使用全部内容,希望文章能够帮你解决cocos2dx 3.2 利用clippingNode把图片裁剪成圆形,接口可直接使用所遇到的程序开发问题。
如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。
本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。