Using Core Graphics in Sprite Kit

1k Views Asked by At

I am using Sprite Kit. I have a shape with the following Core Graphics code:

CGMutablePathRef pathRef = CGPathCreateMutable();
CGPathMoveToPoint(pathRef, NULL, 300, 300);
CGPathAddLineToPoint(pathRef, NULL, 400, 300);
CGPathAddCurveToPoint(pathRef, NULL, 400, 335.727, 380.94, 368.739, 350, 386.603);
CGPathCloseSubpath(pathRef);

CGContextSetRGBFillColor(ctx, 1, 1, 1, 1);
CGContextAddPath(ctx, pathRef);
CGContextFillPath(ctx);

CGContextSetLineWidth(ctx, 1);
CGContextSetRGBStrokeColor(ctx, 0, 0, 0, 1);
CGContextAddPath(ctx, pathRef);
CGContextStrokePath(ctx);

CGPathRelease(pathRef);

I notice when I add it to a SKShapeNode, it looses it's fill and stroke color. I also notice when I scale this object, it treats it like a SpriteNode, and not a vector.

I know you can adjust the SKShapeNode's stroke, fill, stroke width, lineCap, and other things.

I would like the best of both worlds. How could I get the complete shape, with fills, and strokes from CoreGraphics into a SKNode so I can translate it and receive mouse click events?

Thanks for your help.

1

There are 1 best solutions below

2
On BEST ANSWER

Core Graphics properties are ignored, only those on the shape node are applied.

SKShapeNode essentially just renders the path onto a texture, henceforth the shape node is no different than a sprite.

This is just how it is. Trading some features for better rendering performance.