【问题标题】:Draw Rectangle/ Circle and Triangle in Spritekit with Two Colors. . .在 Spritekit 中用两种颜色绘制矩形/圆形和三角形。 . .
【发布时间】:2013-09-30 18:08:19
【问题描述】:

我可以使用简单的 SKSpriteNode 绘制矩形。但我无法在其中绘制其他类型的图纸,例如具有两种分色的三角形、圆形等。有人建议使用 CGPath。但我是新手,不知道画这种复杂的东西。请任何人都可以用 SPRITEKIT 中的 MULTICOLOR 说明使用这些图纸的简单方法。表示它们的上部是一种颜色,下部是第二种颜色。更简洁地说,Shape分为两种颜色,无论是星形、矩形、三角形还是其他。 任何帮助将不胜感激。

谢谢。

【问题讨论】:

    标签: core-graphics ios7 sprite cgpath sprite-kit


    【解决方案1】:

    您可以使用 SKShapeNode 在 sprite kit 中绘制形状,但每个 SKShapeNode 仅限于一种线条颜色(strokeColor)和一种填充颜色。

    但是,您可以创建一个自定义 SKNode 子类,其中包含两个 SKShapeNode 作为子节点,每个子节点具有不同的 strokeColors/fillColors。

    这样的方法适用于自定义 SKNode,它绘制一个左上角为红色、右下角为绿色的正方形:

    - (id) init {
        if (self = [super init]) {
            SKShapeNode* topLeft = [SKShapeNode node];
            UIBezierPath* topLeftBezierPath = [[UIBezierPath alloc] init];
            [topLeftBezierPath moveToPoint:CGPointMake(0.0, 0.0)];
            [topLeftBezierPath addLineToPoint:CGPointMake(0.0, 100.0)];
            [topLeftBezierPath addLineToPoint:CGPointMake(100.0, 100.0)];
            topLeft.path = topLeftBezierPath.CGPath;
            topLeft.lineWidth = 10.0;
            topLeft.strokeColor = [UIColor redColor];
            topLeft.antialiased = NO;
            [self addChild:topLeft];
    
            SKShapeNode* bottomRight = [SKShapeNode node];
            UIBezierPath* bottomRightBezierPath = [[UIBezierPath alloc] init];
            [bottomRightBezierPath moveToPoint:CGPointMake(0.0, 0.0)];
            [bottomRightBezierPath addLineToPoint:CGPointMake(100.0, 0.0)];
            [bottomRightBezierPath addLineToPoint:CGPointMake(100.0, 100.0)];
            bottomRight.path = bottomRightBezierPath.CGPath;
            bottomRight.lineWidth = 10.0;
            bottomRight.strokeColor = [UIColor greenColor];
            bottomRight.antialiased = NO;
            [self addChild:bottomRight];
        }
        return self;
    }
    

    【讨论】:

    • 正如我所说,绘图将填充两种颜色。我怎样才能填充线条。那必须是矩形或其他一次。我承认这是绘画的方式,但我只是想问我如何填充它?
    • 我不明白你的意思。你能上传一张你想要的照片吗?
    • 每个 SKShapeNode 也有一个 fillColor 属性。您可以使用它来设置形状内部的颜色。 developer.apple.com/library/ios/documentation/SpriteKit/…
    • 谢谢。这就是答案。
    猜你喜欢
    • 1970-01-01
    • 2014-12-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多