用两种颜色在 spritekit 中绘制矩形/圆形和三角形. .



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

谢谢。

您可以使用 SKShapeNode 在精灵套件中绘制形状,但每个 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;
}

最新更新