iOS SpriteKit如何使笔触颜色透明?

时间:2014-10-28 11:01:48

标签: ios colors sprite-kit transparency stroke

我现在使用SpriteKit做一些简单的游戏。绘图过程中笔画颜色存在一个问题。

我必须将我的彩色shapeNode透明,但我的笔触颜色每次都保持不变。我已经尝试了不同的技术来使我的节点完全透明:1)将alpha组件设置为描边颜色2)将alpha组件设置为整个节点。它没有任何帮助。有没有办法实现或解决这个问题?

创建我的节点

CGMutablePathRef arc = CGPathCreateMutable();
    cardWidth = cardWidth + cardAlpha;
CGPathAddRoundedRect(arc, NULL, CGRectMake(-cardWidth/2, -cardHeight/2, cardWidth, cardHeight), roundRadius, roundRadius);
roundRect = [SKShapeNode node];
roundRect.name = self.name;
CGFloat lineWidth = 2.0;
CGPathRef strokedArc =
CGPathCreateCopyByStrokingPath(arc, NULL,
                                   lineWidth,
                                   kCGLineCapButt,
                                   kCGLineJoinMiter, // the default
                                   10); // 10 is default miter limit


roundRect.path = strokedArc;
[self addChild:roundRect];

然后我尝试改变颜色和不透明度

roundRect.fillColor = rightColor;
roundRect.strokeColor = rightColor;
roundRect.strokeColor = rightColor;
termLabel.fontColor = rightColor;
roundRect.alpha = 0.5;

1 个答案:

答案 0 :(得分:2)

我认为问题出在你的道路上。填充颜​​色覆盖您的行程,因此您看不到笔触颜色的任何变化。但是通过我的测试,node.alpha组件应该可以工作。它可能是iOS版本的类型,Apple可能会改变一些事物的协同工作方式。

以下是一些可以使用的代码:

    CGMutablePathRef arc = CGPathCreateMutable();
    CGFloat cardWidth = 100;
    CGFloat cardHeight = 170;
    CGFloat roundRadius = 10;
    CGFloat r,g,b = 1; // color components
    CGRect rect = CGRectMake(-cardWidth/2, -cardHeight/2, cardWidth, cardHeight);
    CGPathAddRoundedRect(arc, NULL, rect, roundRadius, roundRadius);
    __block SKShapeNode *roundRect = [SKShapeNode node];
    roundRect.name = @"card";
    CGFloat lineWidth = 20.0;
    CGPathRef strokedArc =
    CGPathCreateCopyByStrokingPath(arc, NULL,
                                   lineWidth,
                                   kCGLineCapButt,
                                   kCGLineJoinMiter, // the default
                                   10); // 10 is default miter limit

    roundRect.path = strokedArc;
//    roundRect.alpha = 0.3; // uncomment if want to use.
    [scene addChild:roundRect];

    // delay
    SKAction *waitAction = [SKAction waitForDuration:1/15.0f];

    // variables
    static CGFloat direction = 1;
    static CGFloat speed = .1f;
    static CGFloat alpha = 0;

    // action to aniamte shape
    SKAction *changeColorAction = [SKAction runBlock:^{
        CGFloat vel = direction * speed;
        CGFloat newValue = alpha + vel;
        if(newValue < 0 || newValue > 1){
            direction *= -1;
        } else {
            alpha = newValue;
        }

        UIColor *newColor = [UIColor colorWithRed:r green:g blue:b alpha:alpha];

        // animate shape
        [roundRect setStrokeColor:newColor];
//        [roundRect setFillColor:newColor]; // uncoment to animate.
    }];

    // Did SKAction because its under the scene run loop.
    SKAction *seq = [SKAction sequence:@[ waitAction, changeColorAction ]];
    [scene runAction:[SKAction repeatActionForever:seq]];