如何绘制圆形条形?

发布于 2024-12-12 06:26:12 字数 154 浏览 0 评论 0原文

我正在用 cocos2d-iphone 开发一个游戏。

我想要一个很棒的圆形健康栏。想想王国之心之类的。

我可以使用 ccDrawLine 绘制圆圈,但它们是完整的圆圈。基本上,我需要能够绘制出一定的周长值来正确表示健康状况。不过,我对此不太确定。有什么想法吗?

I am devleoping a game with cocos2d-iphone.

I want to great a circular health bar. Think of Kingdom Hearts or something.

I am able to draw circles with ccDrawLine, but they are full circles. Basically, I need to be able to draw up to a certain circumference value to represent the health properly. However, I am not really sure about this. Any ideas?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

注定孤独终老 2024-12-19 06:26:12

我快速浏览了 ccDrawCircle 的代码。如果我正在接近这个,我可能会首先修改 for 循环的工作方式(也许通过使用 coefsegs),这样它提前停止了。

void ccDrawCircle( CGPoint center, float r, float a, NSUInteger segs, BOOL drawLineToCenter)
{
    int additionalSegment = 1;
    if (drawLineToCenter)
        additionalSegment++;

    const float coef = 2.0f * (float)M_PI/segs;

    GLfloat *vertices = calloc( sizeof(GLfloat)*2*(segs+2), 1);
    if( ! vertices )
        return;

    for(NSUInteger i=0;i<=segs;i++)
    {
        float rads = i*coef;
        GLfloat j = r * cosf(rads + a) + center.x;
        GLfloat k = r * sinf(rads + a) + center.y;

        vertices[i*2] = j * CC_CONTENT_SCALE_FACTOR();
        vertices[i*2+1] =k * CC_CONTENT_SCALE_FACTOR();
    }
    vertices[(segs+1)*2] = center.x * CC_CONTENT_SCALE_FACTOR();
    vertices[(segs+1)*2+1] = center.y * CC_CONTENT_SCALE_FACTOR();

    // Default GL states: GL_TEXTURE_2D, GL_VERTEX_ARRAY, GL_COLOR_ARRAY, GL_TEXTURE_COORD_ARRAY
    // Needed states: GL_VERTEX_ARRAY, 
    // Unneeded states: GL_TEXTURE_2D, GL_TEXTURE_COORD_ARRAY, GL_COLOR_ARRAY   
    glDisable(GL_TEXTURE_2D);
    glDisableClientState(GL_TEXTURE_COORD_ARRAY);
    glDisableClientState(GL_COLOR_ARRAY);

    glVertexPointer(2, GL_FLOAT, 0, vertices);  
    glDrawArrays(GL_LINE_STRIP, 0, (GLsizei) segs+additionalSegment);

    // restore default state
    glEnableClientState(GL_COLOR_ARRAY);
    glEnableClientState(GL_TEXTURE_COORD_ARRAY);
    glEnable(GL_TEXTURE_2D);    

    free( vertices );
}

I had a quick look at the code for ccDrawCircle. If I was approaching this, I'd probably start by modifying the way the for loop works (maybe by playing with coef or segs) so that it stops early.

void ccDrawCircle( CGPoint center, float r, float a, NSUInteger segs, BOOL drawLineToCenter)
{
    int additionalSegment = 1;
    if (drawLineToCenter)
        additionalSegment++;

    const float coef = 2.0f * (float)M_PI/segs;

    GLfloat *vertices = calloc( sizeof(GLfloat)*2*(segs+2), 1);
    if( ! vertices )
        return;

    for(NSUInteger i=0;i<=segs;i++)
    {
        float rads = i*coef;
        GLfloat j = r * cosf(rads + a) + center.x;
        GLfloat k = r * sinf(rads + a) + center.y;

        vertices[i*2] = j * CC_CONTENT_SCALE_FACTOR();
        vertices[i*2+1] =k * CC_CONTENT_SCALE_FACTOR();
    }
    vertices[(segs+1)*2] = center.x * CC_CONTENT_SCALE_FACTOR();
    vertices[(segs+1)*2+1] = center.y * CC_CONTENT_SCALE_FACTOR();

    // Default GL states: GL_TEXTURE_2D, GL_VERTEX_ARRAY, GL_COLOR_ARRAY, GL_TEXTURE_COORD_ARRAY
    // Needed states: GL_VERTEX_ARRAY, 
    // Unneeded states: GL_TEXTURE_2D, GL_TEXTURE_COORD_ARRAY, GL_COLOR_ARRAY   
    glDisable(GL_TEXTURE_2D);
    glDisableClientState(GL_TEXTURE_COORD_ARRAY);
    glDisableClientState(GL_COLOR_ARRAY);

    glVertexPointer(2, GL_FLOAT, 0, vertices);  
    glDrawArrays(GL_LINE_STRIP, 0, (GLsizei) segs+additionalSegment);

    // restore default state
    glEnableClientState(GL_COLOR_ARRAY);
    glEnableClientState(GL_TEXTURE_COORD_ARRAY);
    glEnable(GL_TEXTURE_2D);    

    free( vertices );
}
单身狗的梦 2024-12-19 06:26:12

CGContextAddArc() 就可以了。一个例子就说明了一切。

             CGContextAddArc(CGFloat centerX, CGFloat centerY, CGFloat radius, CGFloat startAngle, CGFloat endAngle, int clockwise);

我不太确定参数的顺序,你最好用谷歌搜索一下或者让 XCode 帮你完成工作。

CGContextAddArc() will do the trick. An example explains everything.

             CGContextAddArc(CGFloat centerX, CGFloat centerY, CGFloat radius, CGFloat startAngle, CGFloat endAngle, int clockwise);

I'm not quite sure about the order of the parameters, you'd better google it or let XCode do the work for you.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文