Cocos2d-x之绘制矩形

Cocos2d-x之绘制矩形

自定义的方法

Rect.h

 1 //
 2 //  Rect.h
 3 //  L01DrawingAPI
 4 //
 5 //  Created by Mac OS 10.9.3 on 15-3-29.
 6 //
 7 //
 8 
 9 #ifndef __L01DrawingAPI__Rect__
10 #define __L01DrawingAPI__Rect__
11 
12 #include <iostream>
13 #include <cocos2d.h>
14 
15 using namespace cocos2d;
16 
17 namespace bobo {
18     class Rect:public Node{
19     public:
20         virtual bool init();//初始化
21         virtual void draw();//创建绘图
22         CREATE_FUNC(Rect);//创建create方法
23     };
24 };
25 
26 
27 #endif /* defined(__L01DrawingAPI__Rect__) */

Rect.cpp

 1 //
 2 //  Rect.cpp
 3 //  L01DrawingAPI
 4 //
 5 //  Created by Mac OS 10.9.3 on 15-3-29.
 6 //
 7 //
 8 
 9 #include "Rect.h"
10 
11 namespace bobo {
12     //在初始化的方法中可以设置图形的颜色等
13     bool Rect::init(){
14         return true;
15     }
16     //在draw方法中写绘图的操作
17     void Rect::draw(){
18         //设置yanse
19         /********************************
20         此方法中的数值是字节型的,范围是0~255
21         DrawPrimitives::setDrawColor4B(<#GLubyte r#>, <#GLubyte g#>, <#GLubyte b#>, <#GLubyte a#>);
22          ********************************/
23         
24         /********************************
25          此方法中的数值是浮点型的,范围是0~1
26          DrawPrimitives::setDrawColor4F(<#GLfloat r#>, <#GLfloat g#>, <#GLfloat b#>, <#GLfloat a#>);
27          ********************************/
28         
29         //使用字节型的方法,将其设置成红色,参数依次是(红色,绿色,蓝色,透明度)
30         DrawPrimitives::setDrawColor4B(255, 0, 0, 255);
31         //参数中的两个点是对角的两个点
32         DrawPrimitives::drawRect(Point(0, 0), Point(100, 100));
33         
34     }
35 }

在bool HelloWorld::init()方法中添加绘制的矩形

引入头文件:#include "Rect.h"

1 auto r = bobo::Rect::create();//创建图形
2     r->setPosition(Point(50, 50));//设置图形的位置
3     addChild(r);//将创建的图形放在当前的层中
原文地址:https://www.cnblogs.com/dudu580231/p/4375714.html