Cocos2d v3:你如何绘制弧线?

时间:2014-06-29 09:43:01

标签: ios objective-c cocos2d-iphone

我想画一个这样的填充弧:

enter image description here

CCDrawNode只包含绘制圆形,多边形或直线的方法 - 但不包含圆弧。

要清楚,我希望能够在运行时生成弧,具有任意半径和弧角。

我的问题:

有没有办法让Cocos2d绘制弧线,还是我必须自己使用OpenGL来做?

2 个答案:

答案 0 :(得分:1)

我猜您可以使用CCProgressNode,并通过将精灵设置为橙色圆圈,您可以通过设置进度属性来绘制圆弧。

现在,无论你是否可以将精灵设置为可以缩放的矢量化圆圈,我都不太确定。

就个人而言,我建议您将绘图弧代码添加到CCDrawNode,然后提交PR。

答案 1 :(得分:0)

使用cocos2dx v3.8测试,来自DrawNode :: drawSolidCircle的代码

DrawNode* Pie::drawPie(const Vec2& center, float radius, float startAngle, float endAngle, unsigned int segments, float scaleX, float scaleY, const Color4F &color){
    segments++;
    auto draw = DrawNode::create();
    const float coef = (endAngle - startAngle) / segments;

    Vec2 *vertices = new (std::nothrow) Vec2[segments];
    if (!vertices)
        return nullptr;

    for (unsigned int i = 0; i < segments - 1; i++)
    {
        float rads = i*coef;
        GLfloat j = radius * cosf(rads + startAngle) * scaleX + center.x;
        GLfloat k = radius * sinf(rads + startAngle) * scaleY + center.y;

        vertices[i].x = j;
        vertices[i].y = k;
    }

    vertices[segments - 1].x = center.x;
    vertices[segments - 1].y = center.y;

    draw->drawSolidPoly(vertices, segments, color);

    CC_SAFE_DELETE_ARRAY(vertices);

    return draw;
}

称之为

auto pie = Pie::drawPie(_visibleSize / 2, _visibleSize.width / 4, 0, 1, 999, 1, 1, Color4F::BLUE);
相关问题