通过重写draw函数,来实现根据手势划线的功能,代码一看就能明白
#ifndef __HELLOWORLD_SCENE_H__
#define __HELLOWORLD_SCENE_H__
#include "cocos2d.h"
USING_NS_CC;
struct Line
{
Vec2 p1;
Vec2 p2;
};
class HelloWorld : public cocos2d::Layer
{
public:
// there's no 'id' in cpp, so we recommend returning the class instance pointer
static cocos2d::Scene* createScene();
// Here's a difference. Method 'init' in cocos2d-x returns bool, instead of returning 'id' in cocos2d-iphone
virtual bool init();
// implement the "static create()" method manually
CREATE_FUNC(HelloWorld);
public:
virtual bool onTouchBegan(Touch *touch, Event *unused_event);
virtual void onTouchMoved(Touch *touch, Event *unused_event);
virtual void onTouchEnded(Touch *touch, Event *unused_event);
virtual void draw(cocos2d::Renderer *renderer, const cocos2d::Mat4& transform, uint32_t flags);
private:
Vec2 preVec2;
Vec2 curVec2;
Line _line;
std::vector<Line> lines; //通过一段段小的line来画连续的线
};
#endif // __HELLOWORLD_SCENE_H__
实现
#include "HelloWorldScene.h"
#include "cocostudio/CocoStudio.h"
#include "ui/CocosGUI.h"
USING_NS_CC;
using namespace cocostudio::timeline;
Scene* HelloWorld::createScene()
{
// 'scene' is an autorelease object
auto scene = Scene::create();
// 'layer' is an autorelease object
auto layer = HelloWorld::create();
// add layer as a child to scene
scene->addChild(layer);
// return the scene
return scene;
}
// on "init" you need to initialize your instance
bool HelloWorld::init()
{
//
// 1. super init first
if ( !Layer::init() )
{
return false;
}
auto listener = EventListenerTouchOneByOne::create();
listener->onTouchBegan = CC_CALLBACK_2(HelloWorld::onTouchBegan, this);
listener->onTouchMoved = CC_CALLBACK_2(HelloWorld::onTouchMoved, this);
listener->onTouchEnded = CC_CALLBACK_2(HelloWorld::onTouchEnded, this);
Director::getInstance()->getEventDispatcher()->addEventListenerWithSceneGraphPriority(listener, this);
/*auto drawNode = DrawNode::create();
this->addChild(drawNode);
drawNode->drawTriangle(Vec2(20, 250), Vec2(100, 300), Vec2(50, 200), Color4F(1, 1, 0, 1));*/
return true;
}
bool HelloWorld::onTouchBegan(Touch *touch, Event *unused_event) {
auto location = touch->getLocation();
preVec2 = curVec2 = location;
return true;
}
void HelloWorld::onTouchMoved(Touch *touch, Event *unused_event) {
auto location = touch->getLocation();
curVec2 = location;
if ((curVec2 - preVec2).getLength() > 1) {
_line.p1 = preVec2;
_line.p2 = curVec2;
lines.push_back(_line);
preVec2 = curVec2;
}
}
void HelloWorld::onTouchEnded(Touch *touch, Event *unused_event) {
auto location = touch->getLocation();
curVec2 = location;
if ((curVec2 - preVec2).getLength() > 25) {
_line.p1 = preVec2;
_line.p2 = curVec2;
lines.push_back(_line);
preVec2 = curVec2;
}
}
void HelloWorld::draw(cocos2d::Renderer *renderer, const cocos2d::Mat4& transform, uint32_t flags) {
DrawPrimitives::setDrawColor4B(0, 255, 255, 255); ///4B 红 绿 蓝 alpha通道 四个字节(32位 RGBA分别8位 )来表示一个象素包含的信息
glLineWidth(5);
auto iter = lines.begin();
for (iter; iter != lines.end(); iter++) {
DrawPrimitives::drawLine(iter->p1, iter->p2);
}
//DrawPrimitives::drawPoly()
//ccDrawRect(Vec2(10, 10), Vec2(100, 100));
}
运行结果如下图所示: