Cocos2d-x之绘制矩形

本文介绍如何在Cocos2d-x中自定义绘制矩形,通过创建Rect类并实现绘图方法,设置颜色及绘制位置,适用于游戏开发初学者。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值