cocos2dx DrawNode

cocos2dx 两种绘图方式

  • DrawPrimitives
  • DrawNode

DrawPrimitives

3.x 已经弃用

绘制的图形可以是实心的,也可以是空心的。

DrawNode

在一个单独的批处理中绘制了所以元素,因此它绘制点、线段、多边形都要比“drawing primitives”快。

绘制的图形都是实心的。

使用方法

local node = cc.DrawNode:create()
node:clear()
ret:addChild(node)

基本的图形绘制

使用DrawNode通常用来绘制如下几种图形

圆点

drawDot

// ('位置' , '圆点半径' , '填充颜色')
void drawDot(const Vec2 &pos, float radius, const Color4F &color);

例子

auto dot=DrawNode::create();
dot->drawDot(Vec2(0,0), 5, Color4F(1.0, 1.0, 1.0, 1.0));
rect->addChild(dot); 
线段

drawSegment

// ('起点' , '终点' , '半线宽' , '填充颜色')
void drawSegment(const Vec2 &from, const Vec2 &to, float radius, const Color4F &color);

示例

auto segment=DrawNode::create();
segment->drawSegment(Vec2(0, 0), Vec2(20, 20), 3.0, Color4F(0.4,0.6,0.8,1));
this->addChild(segment);
三角形

drawTriangle

//('顶点1' , '顶点2' , '顶点3' , '填充颜色')
void drawTriangle(const Vec2 &p1, const Vec2 &p2, const Vec2 &p3, const Color4F &color);

示例

auto triangle=DrawNode::create();
triangle->drawTriangle(Vec2(20,11), Vec2(133,44), Vec2(44,133), Color4F(0.5,0.6,0.7,1));
this->addChild(triangle);
多边形

drawPolygon

// ('顶点数组' , '顶点个数' , '填充颜色' , '轮廓粗细' , '轮廓颜色')
void drawPolygon(Vec2 *verts, int count, const Color4F &fillColor, float borderWidth, const Color4F &borderColor);

示例

auto polygon=DrawNode::create();
Vec2 verts[]={Vec2(12,22),Vec2(56,66),Vec2(88,98),Vec2(124,54),Vec2(144,88)};//顶点数组
polygon->drawPolygon(verts, 5, Color4F(1.0,0,0,1), 2, Color4F(0,0,1.0,1));
this->addChild(polygon);
二次贝塞尔图形

drawQuadraticBezier

drawQuadBezier

//   ('起点' , '控制点' , '终点' , '分段数' , '填充颜色')
void drawQuadraticBezier(const Vec2& from, const Vec2& control, const Vec2& to, unsigned int segments, const Color4F &color);

void drawQuadBezier(const Vec2 &origin, const Vec2 &control, const Vec2 &destination, unsigned int segments, const Color4F &color);

示例

auto quad=DrawNode::create();
quad->drawQuadBezier(Vec2(12,10), Vec2(22,33), Vec2(111,111), 20, Color4F(0.1,1.0,0.2,1));
this->addChild(quad);
三次贝塞尔图形

drawCubicBezier

// ('起点' , '控制点1' , '控制点2' , '终点' , '分段数' , '填充颜色')
void drawCubicBezier(const Vec2& from, const Vec2& control1, const Vec2& control2, const Vec2& to, unsigned int segments, const Color4F &color);

示例

auto cubic=DrawNode::create();
cubic->drawCubicBezier(Vec2(0,0), Vec2(33,23), Vec2(75,96), Vec2(44,33), 22, Color4F(0.7,0,0,1));
this->addChild(cubic);

Vec2 from2 = Vec2(300, 100);
Vec2 to2 = Vec2(450, 100);
Vec2 control1 = Vec2(350, 0);
Vec2 control2 = Vec2(400, 200);
drawNode->drawCubicBezier(from2, control1, control2, to2, 100, Color4F::YELLOW);
实心矩形

drawSolidRect

// ('原点' , ‘终点’ ,'填充颜色')
void drawSolidRect(const Vec2 &origin, const Vec2 &destination, const Color4F &color);
画圆

drawCircle

//(‘原点’,‘半径’,‘弧度’,‘分段(越大越接近圆)’,‘原点到弧度的线(bool)’,‘线条x缩放’,‘线条y缩放’,‘颜色’)
void drawCircle(const Vec2 &center, float radius, float angle, unsigned int segments, bool drawLineToCenter, const Color4F &color);

void drawCircle( const Vec2& center, float radius, float angle, unsigned int segments, bool drawLineToCenter, float scaleX, float scaleY, const Color4F &color);

示例

auto circle=DrawNode::create();
circle->drawCircle(Vec2(0, 0), 100, 45, 100, false, 1.0, 1.0, Color4F(1.0,1.0,1.0,1));
this->addChild(circle);
绘制指定曲率的曲线

drawCardinalSpline

//congfig:点数组
//tension:张力
//segments:段落
//color:颜色
 void drawCardinalSpline(PointArray *config, float tension,  unsigned int segments, const Color4F &color);

示例

auto cardinalspline=DrawNode::create();
auto array = PointArray::create(4);
array->addControlPoint(Vec2(20, 20));
array->addControlPoint(Vec2(40, 10));
array->addControlPoint(Vec2(-10, 10));
array->addControlPoint(Vec2(60, -10));
array->addControlPoint(Vec2(10, 50));
cardinalspline->drawCardinalSpline(array, 3, 10, Color4F(1.0,1.0,1.0,1));
this->addChild(cardinalspline);
绘制默认曲率的曲线

drawCatmullRom

void drawCatmullRom(PointArray *points, unsigned int segments, const Color4F &color);

示例

auto cat=DrawNode::create();
cat->drawCatmullRom(array, 20, Color4F(1.0, 1.0, 1.0, 1.0));
this->addChild(cat);
绘制直线

drawLine

//('起点','终点','颜色')
void drawLine(const Vec2 &origin, const Vec2 &destination, const Color4F &color);

示例

auto line=DrawNode::create();
line->drawLine(Vec2(33,33), Vec2(222,222), Color4F(1.0,1.0,0.2,1));
this->addChild(line);

你可能感兴趣的:(cocos2dx学习之路,cocos2d,游戏引擎)