在cocos2d中连续绘制渐变线

时间:2012-01-05 11:25:24

标签: iphone ios opengl-es cocos2d-iphone

我搜索过并找不到任何对我有用的东西。如何在cocos2d-iphone中用渐变绘制连续线?我已经尝试了CCRibbon,但后来我得到了间隙,我试图在draw方法中绘制多个对齐的行,但是使用不同的alpha值,但我的draw方法对设置alpha没有反应(它始终是100%alpha,请参阅我的这里绘制方法)。我该怎么办呢?

- (void)draw {
    glEnable(GL_LINE_SMOOTH);
    glColor4ub(0,255,255,50);
    ccDrawLine( ccp(0 - 5, 0 - 5), ccp(200 - 5, 300 - 5) );
}

谢谢你 索伦

2 个答案:

答案 0 :(得分:1)

我不确定这是否是您所需要的:

- (void)draw {
    static GLubyte alpha = 0;
    static int step = 1;

    alpha += step;
    if (alpha == 255 || alpha == 0) {
        step = -step;
    }

    // You gonna need these two lines to use Alpha channel
    glEnable(GL_BLEND); // disabled by default
    glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);

    glEnable(GL_LINE_SMOOTH);
    glColor4ub(0, 255, 255, alpha);
    ccDrawLine(ccp(0, 0), ccp(200, 300));
}

有关GL_BLENDglBlendFunc的详情,请参阅OpenGL ES 1.1 Reference

答案 1 :(得分:0)

从我想要的问题中,例如,从左到右的粗线和从该线的顶部到底部的渐变。如果是这样,那么我就是这样做的。我创建了一个1 x 6 png图像,该图像具有垂直渐变,其中内置了alpha。然后,当我想创建一条线时,我会在我的图层中调用我的drawLine方法。

- (void) drawLine: (CGPoint)origin withEnd:(CGPoint)end
{
    CCSprite *wall = [CCSprite spriteWithFile:@"WallGradient.png"];

    float distance = sqrt(powf(origin.x - end.x, 2) + powf(origin.y - end.y, 2));
    float rotation = (180/M_PI) * acosf((origin.x - end.x) / distance));

    [wall setScaleX:distance];
    [wall setRotation: rotation];
    [wall setPosition: origin];

    [self addChild:wall];
}