An SKShapeNode object draws a shape defined by a Core Graphics path.
The graphics path is a collection of straight lines and curves that
can define either open or closed subpaths. You can specify separate
rendering behavior for the filled and stroked portion of the path.
Each part can be rendered using either a solid color or a texture; if
you need to render more sophisticated effects, you can also use a
custom shader.
Shape nodes are useful for content that cannot be easily decomposed
into simple textured sprites. Shape nodes are also very useful for
building and displaying debugging information on top of your game
content. However, the SKSpriteNode class offers higher performance
than this class, so use shape nodes sparingly.
Creating a shape node from a path shows an example of how to create a
shape node. The example creates a circle with a blue interior and a
white outline. The path is created and attached to the shape node’s
path property.
You can find more details to the Apple official guide
This is the actual available init methods from the sources:
/* Create a Shape Node using a CGPathRef, optionally centered at the Node's origin. */
@available(iOS 8.0, *)
public convenience init(path: CGPath)
@available(iOS 8.0, *)
public convenience init(path: CGPath, centered: Bool)
/* Create a Shape Node representing a Rect. */
@available(iOS 8.0, *)
public convenience init(rect: CGRect)
/* Create a Shape Node representing a rect centered at the Node's origin. */
@available(iOS 8.0, *)
public convenience init(rectOfSize size: CGSize)
/* Create a Shape Node representing a rounded rect with a corner radius */
@available(iOS 8.0, *)
public convenience init(rect: CGRect, cornerRadius: CGFloat)
/* Create a Shape Node representing a rounded rect with a corner radius centered at the Node's origin. */
@available(iOS 8.0, *)
public convenience init(rectOfSize size: CGSize, cornerRadius: CGFloat)
/* Create a Shape Node representing an circle centered at the Node's origin. */
@available(iOS 8.0, *)
public convenience init(circleOfRadius radius: CGFloat)
/* Create a Shape Node representing an Ellipse inscribed within a Rect */
@available(iOS 8.0, *)
public convenience init(ellipseInRect rect: CGRect)
/* Create a Shape Node representing an Ellipse inscribed within a Rect centered at the Node's origin. */
@available(iOS 8.0, *)
public convenience init(ellipseOfSize size: CGSize)
/* Create a Shape Node representing an a series of Points interpreted as line segments */
@available(iOS 8.0, *)
public convenience init(points: UnsafeMutablePointer<CGPoint>, count numPoints: Int)
/* Create a Shape Node representing a smoothed spline that passes through a series of Points */
@available(iOS 8.0, *)
public convenience init(splinePoints points: UnsafeMutablePointer<CGPoint>, count numPoints: Int)
As you can see you can create non only squares but also irregular polygon using for example the a custom CGPath
.
A typical example is this below:
let shape = SKShapeNode()
shape.path = UIBezierPath(roundedRect: CGRect(x: -128, y: -128, width: 256, height: 256), cornerRadius: 64).CGPath
shape.position = CGPoint(x: CGRectGetMidX(frame), y: CGRectGetMidY(frame))
shape.fillColor = UIColor.redColor()
shape.strokeColor = UIColor.blueColor()
shape.lineWidth = 10
addChild(shape)
P.S.:
Could occur some bugs in memory caused by SKShapeNode
during development but nothing that would prevent you to test, create and publish a game, many games on the market make use of SKShapeNode
without problems , you have to simply be more careful in the code without abusing of it.