iOS Developer Library

Developer

SpriteKit Framework Reference SKShapeNode Class Reference

Options
Deployment Target:

On This Page
Language:

SKShapeNode

Inheritance


Conforms To


Import Statement


Swift

import SpriteKit

Objective-C

@import SpriteKit;

Availability


Available in iOS 7.0 and later.

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.

Listing 1Creating a shape node from a path
  • SKShapeNode *ball = [[SKShapeNode alloc] init];
  • CGMutablePathRef myPath = CGPathCreateMutable();
  • CGPathAddArc(myPath, NULL, 0,0, 15, 0, M_PI*2, YES);
  • ball.path = myPath;
  • ball.lineWidth = 1.0;
  • ball.fillColor = [SKColor blueColor];
  • ball.strokeColor = [SKColor whiteColor];
  • ball.glowWidth = 0.5;

You can see from the code that the shape has three essential elements:

  • The interior of the shape is filled. The fillColor property specifies the color used to fill the interior.

  • The outline of the shape is rendered as a line. The strokeColor and lineWidth properties define how the line is stroked.

  • A glow extends from the outline. The glowWidth and strokeColor properties define the glow.

You can disable any of these elements by setting its color to [SKColor clearColor].

The shape node provides properties that let you control how the shape is blended into the framebuffer. You use these properties the same way as the properties of the SKSpriteNode class. See Blending the Sprite into the Framebuffer.

  • Creates a shape node from a Core Graphics path.

    Declaration

    Swift

    convenience init(path path: CGPath!)

    Objective-C

    + (instancetype)shapeNodeWithPath:(CGPathRef)path

    Parameters

    path

    The Core Graphics path to use. The path is relative to the node’s origin.

    Return Value

    A new shape node.

    Import Statement

    Objective-C

    @import SpriteKit;

    Swift

    import SpriteKit

    Availability

    Available in iOS 8.0 and later.

  • Creates a shape node from a Core Graphics path.

    Declaration

    Swift

    convenience init(path path: CGPath!, centered centered: Bool)

    Objective-C

    + (instancetype)shapeNodeWithPath:(CGPathRef)path centered:(BOOL)centered

    Parameters

    path

    The Core Graphics path to use.

    centered

    If YEStrue, the path is translated so that the center of the path’s bounding box is at the node’s origin; otherwise the path is relative to the node’s origin.

    Return Value

    A new shape node.

    Import Statement

    Objective-C

    @import SpriteKit;

    Swift

    import SpriteKit

    Availability

    Available in iOS 8.0 and later.

  • Creates a shape node with a rectangular path.

    Declaration

    Swift

    convenience init(rect rect: CGRect)

    Objective-C

    + (instancetype)shapeNodeWithRect:(CGRect)rect

    Parameters

    rect

    A rectangle, relative to the node’s origin.

    Return Value

    A new shape node.

    Import Statement

    Objective-C

    @import SpriteKit;

    Swift

    import SpriteKit

    Availability

    Available in iOS 8.0 and later.

  • Creates a shape node with a rectangular path centered on the node’s origin.

    Declaration

    Swift

    convenience init(rectOfSize size: CGSize)

    Objective-C

    + (instancetype)shapeNodeWithRectOfSize:(CGSize)size

    Parameters

    size

    The size of the rectangle.

    Return Value

    A new shape node.

    Import Statement

    Objective-C

    @import SpriteKit;

    Swift

    import SpriteKit

    Availability

    Available in iOS 8.0 and later.

  • Creates a shape with a rectangular path with rounded corners.

    Declaration

    Swift

    convenience init(rect rect: CGRect, cornerRadius cornerRadius: CGFloat)

    Objective-C

    + (instancetype)shapeNodeWithRect:(CGRect)rect cornerRadius:(CGFloat)cornerRadius

    Parameters

    rect

    A rectangle, relative to the node’s origin.

    cornerRadius

    The radius of the rounded corners. The radius should not be a negative number. The value should be no larger than half of the rectangle’s width or height, whichever is smaller.

    Return Value

    A new shape node.

    Import Statement

    Objective-C

    @import SpriteKit;

    Swift

    import SpriteKit

    Availability

    Available in iOS 8.0 and later.

  • Creates a shape with a rectangular path with rounded corners centered on the node’s origin.

    Declaration

    Swift

    convenience init(rectOfSize size: CGSize, cornerRadius cornerRadius: CGFloat)

    Objective-C

    + (instancetype)shapeNodeWithRectOfSize:(CGSize)size cornerRadius:(CGFloat)cornerRadius

    Parameters

    size

    The size of the rectangle.

    cornerRadius

    The radius of the rounded corners. The radius should not be a negative number. The value should be no larger than half of the rectangle’s width or height, whichever is smaller.

    Return Value

    A new shape node.

    Import Statement

    Objective-C

    @import SpriteKit;

    Swift

    import SpriteKit

    Availability

    Available in iOS 8.0 and later.

  • Creates a shape node with a circular path centered on the node’s origin.

    Declaration

    Swift

    convenience init(circleOfRadius radius: CGFloat)

    Objective-C

    + (instancetype)shapeNodeWithCircleOfRadius:(CGFloat)radius

    Parameters

    radius

    The radius of the circle.

    Return Value

    A new shape node.

    Import Statement

    Objective-C

    @import SpriteKit;

    Swift

    import SpriteKit

    Availability

    Available in iOS 8.0 and later.

  • Creates a shape node with an elliptical path centered on the node’s origin.

    Declaration

    Swift

    convenience init(ellipseOfSize size: CGSize)

    Objective-C

    + (instancetype)shapeNodeWithEllipseOfSize:(CGSize)size

    Parameters

    size

    The height and width of the ellipse.

    Return Value

    A new shape node.

    Import Statement

    Objective-C

    @import SpriteKit;

    Swift

    import SpriteKit

    Availability

    Available in iOS 8.0 and later.

  • Creates a shape node with an elliptical path that fills the specified rectangle.

    Declaration

    Swift

    convenience init(ellipseInRect rect: CGRect)

    Objective-C

    + (instancetype)shapeNodeWithEllipseInRect:(CGRect)rect

    Parameters

    rect

    A rectangle, relative to the node’s origin.

    Return Value

    A new shape node.

    Import Statement

    Objective-C

    @import SpriteKit;

    Swift

    import SpriteKit

    Availability

    Available in iOS 8.0 and later.

  • Creates a shape node from a series of points.

    Declaration

    Swift

    convenience init(points points: UnsafeMutablePointer<CGPoint>, count numPoints: Int)

    Objective-C

    + (instancetype)shapeNodeWithPoints:(CGPoint *)points count:(size_t)numPoints

    Parameters

    points

    An array of Core Graphics points. The points are relative to the node’s origin.

    numPoints

    The number of points in the array.

    Return Value

    A new shape node. The node is created with a path that starts at the first point in the array, joining each adjacent pair of points with a line segment.

    Import Statement

    Objective-C

    @import SpriteKit;

    Swift

    import SpriteKit

    Availability

    Available in iOS 8.0 and later.

  • Creates a shape node from a series of spline points.

    Declaration

    Swift

    convenience init(splinePoints points: UnsafeMutablePointer<CGPoint>, count numPoints: Int)

    Objective-C

    + (instancetype)shapeNodeWithSplinePoints:(CGPoint *)points count:(size_t)numPoints

    Parameters

    points

    An array of Core Graphics points.

    numPoints

    The number of points in the array.

    Return Value

    A new shape node is created. The node is created with a path that starts at the first point in the array, joining each pair of points with a quadratic curve. The control points are calculated automatically based on previous points in the array.

    Import Statement

    Objective-C

    @import SpriteKit;

    Swift

    import SpriteKit

    Availability

    Available in iOS 8.0 and later.

  • path path Property

    The path that defines the shape.

    Declaration

    Swift

    var path: CGPath!

    Objective-C

    @property(nonatomic) CGPathRef path

    Discussion

    The path is defined in the node’s coordinate space.

    Import Statement

    Objective-C

    @import SpriteKit;

    Swift

    import SpriteKit

    Availability

    Available in iOS 7.0 and later.

  • fillColor fillColor Property

    The color used to fill the shape.

    Declaration

    Swift

    var fillColor: UIColor

    Objective-C

    @property(nonatomic, retain) UIColor *fillColor

    Discussion

    The default fill color is [SKColor clearColor], which means the shape is not filled.

    Import Statement

    Objective-C

    @import SpriteKit;

    Swift

    import SpriteKit

    Availability

    Available in iOS 7.0 and later.

  • The texture used to fill the shape.

    Declaration

    Swift

    var fillTexture: SKTexture?

    Objective-C

    @property(nonatomic, retain) SKTexture *fillTexture

    Discussion

    The default value is nil. If a fill texture is specified, the fillColor property is ignored and the filled portion of the shape node is rendered using the texture instead.

    Import Statement

    Objective-C

    @import SpriteKit;

    Swift

    import SpriteKit

    Availability

    Available in iOS 8.0 and later.

  • A custom shader used to determine the color of the filled portion of the shape node.

    Declaration

    Swift

    var fillShader: SKShader?

    Objective-C

    @property(nonatomic, retain) SKShader *fillShader

    Discussion

    The default value is nil. If a fill shader is specified, when the shape node is drawn, the shader is used to determine the output colors for any part of the shape node that is filled.

    Import Statement

    Objective-C

    @import SpriteKit;

    Swift

    import SpriteKit

    Availability

    Available in iOS 8.0 and later.

  • lineWidth lineWidth Property

    The width used to stroke the path.

    Declaration

    Swift

    var lineWidth: CGFloat

    Objective-C

    @property(nonatomic) CGFloat lineWidth

    Discussion

    A line width larger than 2.0 may cause rendering artifacts in the final rendered image. The default value is 1.0.

    Import Statement

    Objective-C

    @import SpriteKit;

    Swift

    import SpriteKit

    Availability

    Available in iOS 7.0 and later.

  • glowWidth glowWidth Property

    The glow that extends outward from the stroked line.

    Declaration

    Swift

    var glowWidth: CGFloat

    Objective-C

    @property(nonatomic) CGFloat glowWidth

    Discussion

    The default value is 0.0, which means no glow is added.

    Import Statement

    Objective-C

    @import SpriteKit;

    Swift

    import SpriteKit

    Availability

    Available in iOS 7.0 and later.

  • A Boolean value that determines whether the stroked path is smoothed when drawn.

    Declaration

    Swift

    var antialiased: Bool

    Objective-C

    @property(nonatomic, getter=isAntialiased) BOOL antialiased

    Discussion

    The default value is YEStrue.

    Import Statement

    Objective-C

    @import SpriteKit;

    Swift

    import SpriteKit

    Availability

    Available in iOS 7.0 and later.

  • The color used to stroke the shape.

    Declaration

    Swift

    var strokeColor: UIColor

    Objective-C

    @property(nonatomic, retain) UIColor *strokeColor

    Discussion

    The default stroke color is [SKColor whiteColor]. If you do not want to stroke the shape, use [SKColor clearColor].

    Import Statement

    Objective-C

    @import SpriteKit;

    Swift

    import SpriteKit

    Availability

    Available in iOS 7.0 and later.

  • The texture used to stroke the shape.

    Declaration

    Swift

    var strokeTexture: SKTexture?

    Objective-C

    @property(nonatomic, retain) SKTexture *strokeTexture

    Discussion

    The default value is nil. If a stroke texture is specified, the strokeColor property is ignored and the stroked portion of the shape node is rendered using the texture instead.

    Import Statement

    Objective-C

    @import SpriteKit;

    Swift

    import SpriteKit

    Availability

    Available in iOS 8.0 and later.

  • A custom shader used to determine the color of the stroked portion of the shape node.

    Declaration

    Swift

    var strokeShader: SKShader?

    Objective-C

    @property(nonatomic, retain) SKShader *strokeShader

    Discussion

    The default value is nil. If a stroke shader is specified, when the shape node is drawn, the shader is used to determine the output colors for any part of the shape node that is stroked.

    Import Statement

    Objective-C

    @import SpriteKit;

    Swift

    import SpriteKit

    Availability

    Available in iOS 8.0 and later.

  • lineCap lineCap Property

    The style used to render the endpoints of the stroked portion of the shape node.

    Declaration

    Swift

    var lineCap: CGLineCap

    Objective-C

    @property(nonatomic) CGLineCap lineCap

    Discussion

    The default value is kCGLineCapButt. See CGLineCap.

    Import Statement

    Objective-C

    @import SpriteKit;

    Swift

    import SpriteKit

    Availability

    Available in iOS 8.0 and later.

  • lineJoin lineJoin Property

    The junction type used when the stroked portion of the shape node is rendered.

    Declaration

    Swift

    var lineJoin: CGLineJoin

    Objective-C

    @property(nonatomic) CGLineJoin lineJoin

    Discussion

    The default value is kCGLineJoinBevel. See CGLineJoin.

    Import Statement

    Objective-C

    @import SpriteKit;

    Swift

    import SpriteKit

    Availability

    Available in iOS 8.0 and later.

    See Also

    miterLimit

  • The miter limit to use when the line is stroked using a miter join style.

    Declaration

    Swift

    var miterLimit: CGFloat

    Objective-C

    @property(nonatomic) CGFloat miterLimit

    Discussion

    If the line join style is set to kCGLineJoinMiter, Sprite Kit uses the miter limit to determine whether the lines should be joined with a bevel instead of a miter. Sprite Kit divides the length of the miter by the line width. If the result is greater than the miter limit, Sprite Kit converts the style to a bevel.

    Import Statement

    Objective-C

    @import SpriteKit;

    Swift

    import SpriteKit

    Availability

    Available in iOS 8.0 and later.

    See Also

    lineJoin

  • blendMode blendMode Property

    The blend mode used to blend the shape into the parent’s framebuffer.

    Declaration

    Swift

    var blendMode: SKBlendMode

    Objective-C

    @property(nonatomic) SKBlendMode blendMode

    Discussion

    The default value is SKBlendModeAlpha.

    Import Statement

    Objective-C

    @import SpriteKit;

    Swift

    import SpriteKit

    Availability

    Available in iOS 7.0 and later.

  • The length of the line defined by the shape node. (read-only)

    Declaration

    Swift

    var lineLength: CGFloat { get }

    Objective-C

    @property(nonatomic, readonly) CGFloat lineLength

    Import Statement

    Objective-C

    @import SpriteKit;

    Swift

    import SpriteKit

    Availability

    Available in iOS 8.0 and later.