cocos2dx中的实现简单的画板功能

3/1/2017来源:Android开发人气:1372

不多说,直接上源代码:

BoardLayer.h :

#include "cocos2d.h"

class BoardLayer :public cocos2d::LayerColor

{

public:

    BoardLayer();

    virtual ~BoardLayer();

    CREATE_FUNC(BoardLayer);

    

    virtual bool init();

    

    virtual bool onTouchBegan(cocos2d::Touch *touch,cocos2d::Event *unused_event);

    virtual void onTouchMoved(cocos2d::Touch *touch,cocos2d::Event *unused_event);

    virtual void onTouchEnded(cocos2d::Touch *touch,cocos2d::Event *unused_event);

    

PRivate:

    cocos2d::Point _originPoint;

    cocos2d::DrawNode* _drawNode;

    cocos2d::Color4F _drawColor;

    float _drawSize;

};

BoardLayer.cpp :

#include "BoardLayer.h"

USING_NS_CC;

BoardLayer::BoardLayer()

{

}

BoardLayer::~BoardLayer()

{

}

bool BoardLayer::init()

{

    Size visibleSize = Director::getInstance()->getVisibleSize();

    if (!LayerColor::initWithColor(Color4B(255,255, 255, 255), visibleSize.width, visibleSize.height))

        return false;

    

    this->setTouchMode(Touch::DispatchMode::ONE_BY_ONE);

    this->setTouchEnabled(true);

    

    _drawNode = DrawNode::create();

    this->addChild(_drawNode);

    

    _drawColor = Color4F(1,0, 0,1);

    _drawSize = 10;

    

    return true;

}

bool BoardLayer::onTouchBegan(cocos2d::Touch *touch,cocos2d::Event *unused_event)

{

    _originPoint = touch->getLocation();

    return true;

}

void BoardLayer::onTouchMoved(cocos2d::Touch *touch,cocos2d::Event *unused_event)

{

    Point location = touch->getLocation();

    

    _drawNode->drawSegment(_originPoint, location,_drawSize / 2,_drawColor);

    _originPoint = location;

}

void BoardLayer::onTouchEnded(cocos2d::Touch *touch,cocos2d::Event *unused_event)

{

    if (_originPoint == touch->getLocation())

        _drawNode->drawPoint(_originPoint,_drawSize, _drawColor);

}

//***************************

主要就是使用DrawNode的drawSegment功能;



蓝月亮 香港马会开奖2019年