关于Cocos2d-x 3.x 版本的绘图方法有两种: 1、使用DrawNode类绘制自定义图形。 2、继承Layer类重写draw()方法。 以上两种方法都可以绘制自定义图形,根据自己的需要选择合适的方法,这里我们只讨论第一种方法,第二种方法涉及到opengl的知识,暂不讨论。 我
关于Cocos2d-x 3.x 版本的绘图方法有两种:
1、使用DrawNode类绘制自定义图形。
2、继承Layer类重写draw()方法。
以上两种方法都可以绘制自定义图形,根据自己的需要选择合适的方法,这里我们只讨论第一种方法,第二种方法涉及到opengl的知识,暂不讨论。
我们先来简单的看看DrawNode提供的API接口:
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
看完上面的API接口后,下面使用起来实在是太方便了。使用DrawNode 类绘制图形是最简单的方法,create一个DrawNode类,然后添加进场景。然后就可以愉快的绘图了:
12345678910111213141516171819202122232425
基本用法就是这么简单,如果需要别的形状,自己组合DIY。
来源网址:http://blog.csdn.net/ac_huang/article/details/39522473
classCC_DLL DrawNode : publicNode{ public: //初始化一个DrawNode对象,然后被addChild添加进去就ok了 staticDrawNode* create(); //画实心圆,参数分别是圆心位置、圆半径、圆填充颜色,如果要画空心圆,就把圆当多边形画(这个多边形点数很多而已) voiddrawDot(constVec2 &pos, floatradius, constColor4F &color); //画线段,从from到to,2*radius是线段的宽度和radius是线段两头半圆形的半径 voiddrawSegment(constVec2 &from, constVec2 &to, floatradius, constColor4F &color); //画多边形,verts为点集,count为点数,fillColor为填充颜色,borderWidth为边缘线宽,borderColor为边缘线颜色 voiddrawPolygon(Vec2 *verts, intcount, constColor4F &fillColor, floatborderWidth, constColor4F &borderColor); //画三角形,三人顶点及其填充色 voiddrawTriangle(constVec2 &p1, constVec2 &p2, constVec2 &p3, constColor4F &color); //画三次贝塞尔曲线 voiddrawCubicBezier(constVec2& from, constVec2& control1, constVec2& control2, constVec2& to, unsigned intsegments, constColor4F &color); //画二次贝塞尔曲线 voiddrawQuadraticBezier(constVec2& from, constVec2& control, constVec2& to, unsigned intsegments, constColor4F &color); /** Clear the geometry in the node's buffer. */ voidclear(); /** * @js NA * @lua NA */ constBlendFunc& getBlendFunc() const; /** * @code * When this function bound into js or lua,the parameter will be changed * In js: var setBlendFunc(var src, var dst) * @endcode * @lua NA */ voidsetBlendFunc(constBlendFunc &blendFunc); voidonDraw(constMat4 &transform, uint32_t flags); // 新的绘图渲染函数 virtualvoiddraw(Renderer *renderer, constMat4 &transform, uint32_t flags) override; CC_CONSTRUCTOR_ACCESS: DrawNode(); virtual~DrawNode(); virtualboolinit(); protected: voidensureCapacity(intcount); GLuint _vao; GLuint _vbo; int_bufferCapacity; GLsizei _bufferCount; V2F_C4B_T2F *_buffer; BlendFunc _blendFunc; CustomCommand _customCommand; bool_dirty; private: CC_DISALLOW_COPY_AND_ASSIGN(DrawNode);};//创建DrawNode对象DrawNode *drawNode = DrawNode::create();//加入场景就OKthis->addChild(drawNode, 20);//画实心圆drawNode->drawDot(Vec2(100, 100), 50, Color4F(0.5,0.6,0,1));//画线段drawNode->drawSegment(Vec2(100,100), Vec2(100,220), 0.5, Color4F(0,1,0,1));// 画多边形Vec2 points[] = { Vec2(s.height/4, 10), Vec2(s.width - 10, s.height/5), Vec2(s.width/3*2,s.height) };drawNode->drawPolygon(points, sizeof(points)/sizeof(points[0]), Color4F(1,0,0,0.5), 2, Color4F(0,0,1,1));// 画三角形drawNode->drawTriangle(Vec2(10, 10), Vec2(70, 30), Vec2(100, 140), Color4F(CCRANDOM_0_1(), CCRANDOM_0_1(), CCRANDOM_0_1(), 0.5));// 画二次贝塞尔曲线drawNode->drawQuadraticBezier(Vec2(s.width - 150, s.height - 150), Vec2(s.width - 70, s.height - 10), Vec2(s.width - 10, s.height - 10), 10, Color4F(CCRANDOM_0_1(), CCRANDOM_0_1(), CCRANDOM_0_1(), 0.5));// 画三次贝塞尔曲线 draw->drawCubicBezier(Vec2(s.width - 250, 40), Vec2(s.width - 70, 100), Vec2(s.width - 30, 250), Vec2(s.width - 10, s.height - 50), 10, Color4F(CCRANDOM_0_1(), CCRANDOM_0_1(), CCRANDOM_0_1(), 0.5));