F
F
FootWork2015-08-05 11:36:02
C++ / C#
FootWork, 2015-08-05 11:36:02

Cocos2d-x-3.7 C++ How to draw a line with mouse?

Hi all! I started working on cocos2d-x-3.7, and I can’t find in Google how to draw a line with a pen (as in paint). It seems that there are some solutions, but too complex and completely incomprehensible.
Here is my hello word code:

//HelloWorldScene.h фрагмент класса HelloWorld
virtual void onTouchEnded(cocos2d::Touch*, cocos2d::Event*);
virtual void onTouchMoved(cocos2d::Touch*, cocos2d::Event*);

//HelloWorldScene.cpp 
void HelloWorld::Init()
{
   //...
   touchListener->onTouchEnded = CC_CALLBACK_2(HelloWorld::onTouchEnded, this);
   touchListener->onTouchMoved = CC_CALLBACK_2(HelloWorld::onTouchMoved, this);
}
void HelloWorld::onTouchEnded(Touch* touch, Event* event)
{
   cocos2d::log("touch ended");
}

void HelloWorld::onTouchMoved(Touch* touches, Event* event)
{
    cocos2d::log("touch moved");
}

Actually, what do I need to add to onTouchMoved so that I can draw a line when the mouse button is pressed?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
F
FootWork, 2015-08-08
@FootWork

class Line
{
public:
  std::vector<cocos2d::CCPoint> curve;
};
class HelloWorld
{
    //.....
private:
    cocos2d::DrawNode *_drawNode;
    std::vector<Line> m_lines;
    Line * newLine;
};
bool HelloWorld::Init()
{
    //...
    _drawNode = DrawNode::create();
    this->addChild(_drawNode);
   _drawNode->clear();
}
bool HelloWorld::onTouchBegan(cocos2d::Touch* touch, cocos2d::Event* event)
{
    CCPoint touchLocation = touch->getLocationInView();
    touchLocation = CCDirector::sharedDirector()->convertToGL(touchLocation);
    touchLocation = convertToNodeSpace(touchLocation);
    createLine(touchLocation);    
   return true;
}

void HelloWorld::onTouchMoved(Touch* touch, Event* event)
{
    CCPoint touchLocation = touch->getLocationInView();
    touchLocation = CCDirector::sharedDirector()->convertToGL(touchLocation);
    touchLocation = convertToNodeSpace(touchLocation);
    addPoint(touchLocation);
    HelloWorld::drawing();
}

void HelloWorld::onTouchEnded(Touch* touch, Event* event)
{
    deleteLine();
}

void HelloWorld::createLine(cocos2d::CCPoint tloc)
{
    newLine = new Line;
    (newLine->curve).push_back(tloc);
}
void HelloWorld::deleteLine()
{
    delete newLine;
    newLine = NULL;
}
void HelloWorld::addPoint(cocos2d::CCPoint tloc)
{
    (newLine->curve).push_back(tloc);
}
void HelloWorld::drawing()
{
    int m = (newLine->curve).size();
    for(int i=1; i < m; i++)
    {
        _drawNode->drawLine(newLine->curve[i-1], newLine->curve[i], Color4F::BLUE);
    }
}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question