Draw Rectangle/ Circle and Triangle in Spritekit with Two Colors. . .

后端 未结 1 1080
佛祖请我去吃肉
佛祖请我去吃肉 2021-01-05 21:34

I can draw Rectangle using simple SKSpriteNode. But i can not draw other types of drawings in it like Triangle, Circle etc with TWO SPLIT COLORS. Someone suggested to go wit

1条回答
  •  旧巷少年郎
    2021-01-05 22:17

    You can use SKShapeNode to draw shapes in sprite kit, but each SKShapeNode is limited to one line color (strokeColor) and one fill color.

    However, you can create a custom SKNode subclass that contains two SKShapeNodes as children, each with different strokeColors/fillColors.

    Something like this will work for a custom SKNode that draws a square with left and top red, right and bottom green:

    - (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;
    }
    

    0 讨论(0)
提交回复
热议问题