iOS SpriteKit如何使笔画颜色透明? [英] iOS SpriteKit How to make stroke color transparent?

查看:257
本文介绍了iOS SpriteKit如何使笔画颜色透明?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I do some simple games using SpriteKit now. There is one problem with stroke color in drawing process.

我必须做我的彩色shapeNode透明,但我的笔画颜色每次都保持不变。我尝试了不同的技术来做我的节点是完全透明的:1)设置alpha组件到笔触颜色2)设置alpha组件到整个节点。它没有帮助。有没有办法实现或绕过这个?

I have to do my colored shapeNode transparent, but my stroke color stays the same every time. I've tried different techniques to do my node be entirely transparent: 1)setting alpha component to stroke color 2)setting alpha component to whole node. It didn't help. Is there is any way to achieve or get around this?

创建我的节点

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];

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

Then i try to change color and opacity

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


推荐答案

我认为问题是你的路。填充颜​​色覆盖你的行程,所以你看不到任何改变的笔触颜色。但是用我的测试,node.alpha组件应该工作。这可能是iOS版本的类型,苹果可能会改变一些事情如何一起工作。

I think the problem is with your path. the fill color is covering your stoke so you don't see any change of the stroke color. But with my test, the node.alpha component should work. It maybe the type of iOS version, Apple may change how some things work together.

这里是一些代码:

    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]];

这篇关于iOS SpriteKit如何使笔画颜色透明?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆