Cocos2d-x v3 Smooth Lines

Hello everyone!

I have a problem drawing smooth lines with my finger :confused: I’m doing the same thing as this guy here:

The only change I made is that in the onTouchesMoved method instead of a CCSet I have a std::vector<Touch*> and instead of

CCTouch *touch = (CCTouch *)touches->anyObject();

I have

 Touch *touch = touches[0];

The rest is the same.
The program works, but the line is not continuous… it has holes. Any idea how to fix this issue? I saw in the comments that other people also have problems with this in the v3 version of cocos2dx.

For cocos2d-x v3.x, you have to create a sprite in each iteration of the loop for, like this:

target->begin();

    float distance = start.distance(end);

    for (int i = 0; i < distance; i++)
    {
        float difx = end.x - start.x;
        float dify = end.y - start.y;
        float delta = (float)i / distance;

        Sprite * sprite = Sprite::create(_brushName);
        sprite->setColor(_drawingColor);
        sprite->setPosition(Vec2(start.x + (difx * delta), start.y + (dify * delta)));
      
        brush->visit();
    }

    target->end();
1 Like

Thanks! But I already managed to fix that problem and I did it with drawing primitives.
Here is the code if someone in the future has the same problem:
OnTouchesMoved method:

Touch *touch = touches[0];
Point start = touch->getLocationInView();
start = Director::getInstance()->convertToGL(start);
Point end = touch->getPreviousLocationInView();
end = Director::getInstance()->convertToGL(end);

if (end.getDistance(start) > 2)
{
    drawCurPoint(start, end);
}

drawCurPoint method:

Point dir = end - start;
dir = dir.getPerp();
Point perpendicular = dir.getNormalized();
Point A =  start + (perpendicular * (DOT_RADIUS / 2));
Point B =  start - (perpendicular * (DOT_RADIUS / 2));
Point C =  end + (perpendicular * (DOT_RADIUS / 2));
Point D =  end - (perpendicular * (DOT_RADIUS / 2));

Point poly[4] = {A, C, D, B};  
   
dot->drawSolidPoly(poly, 4, Color4F::BLACK);
dot->drawDot(end, DOT_RADIUS, Color4F::BLACK);

And by the way… I think that creating everytime the same Sprite* is not that optimal… you should use FrameCache for that if you dont want to kill your program :wink:

1 Like