Mac Developer Library

Developer

SpriteKit Framework Reference SKSpriteNode Class Reference

Options
Deployment Target:

On This Page
Language:

SKSpriteNode

Inheritance


Conforms To


Import Statement


Swift

import SpriteKit

Objective-C

@import SpriteKit;

Availability


Available in OS X v10.9 and later.

An SKSpriteNode is a node that draws a textured image, a colored square, or a textured image blended with a color. You can also provide a custom shader to create your own rendering effects.

  • Initializes a colored sprite.

    Declaration

    Swift

    convenience init(color color: NSColor!, size size: CGSize)

    Objective-C

    - (instancetype)initWithColor:(NSColor *)color size:(CGSize)size

    Parameters

    color

    The color for the resulting sprite.

    size

    The size of the sprite in points.

    Return Value

    A newly initialized sprite object.

    Discussion

    The sprite object that is returned from this method has its texture property set to nil.

    Import Statement

    Objective-C

    @import SpriteKit;

    Swift

    import SpriteKit

    Availability

    Available in OS X v10.9 and later.

  • Initializes a textured sprite using an image file.

    Declaration

    Swift

    convenience init(imageNamed name: String)

    Objective-C

    - (instancetype)initWithImageNamed:(NSString *)name

    Parameters

    name

    The name of an image file stored in the app bundle.

    Return Value

    A newly initialized sprite object.

    Discussion

    This method creates a new texture object from the image file and assigns that texture to the texture property. The size property of the sprite is set to the dimensions of the image. The color property is set to white (1.0,1.0,1.0).

    Import Statement

    Objective-C

    @import SpriteKit;

    Swift

    import SpriteKit

    Availability

    Available in OS X v10.9 and later.

  • Initializes a textured sprite using an existing texture object.

    Declaration

    Swift

    convenience init(texture texture: SKTexture!)

    Objective-C

    - (instancetype)initWithTexture:(SKTexture *)texture

    Parameters

    texture

    A Sprite Kit texture.

    Return Value

    A newly initialized sprite object.

    Discussion

    The size property of the sprite is set to the dimensions of the texture. The color property is set to white (1.0,1.0,1.0).

    Import Statement

    Objective-C

    @import SpriteKit;

    Swift

    import SpriteKit

    Availability

    Available in OS X v10.9 and later.

  • Initializes a colored and textured sprite using an existing texture object.

    Declaration

    Swift

    init(texture texture: SKTexture!, color color: NSColor!, size size: CGSize)

    Objective-C

    - (instancetype)initWithTexture:(SKTexture *)texture color:(NSColor *)color size:(CGSize)size

    Parameters

    texture

    A texture to apply to the sprite.

    color

    The color for the new sprite.

    size

    The size for the new sprite.

    Return Value

    A newly initialized sprite object.

    Discussion

    To colorize your texture, you also need to set the colorBlendFactor property of the sprite.

    Import Statement

    Objective-C

    @import SpriteKit;

    Swift

    import SpriteKit

    Availability

    Available in OS X v10.9 and later.

    See Also

    colorBlendFactor

  • Creates and returns a colored sprite.

    Declaration

    Objective-C

    + (instancetype)spriteNodeWithColor:(NSColor *)color size:(CGSize)size

    Parameters

    color

    The color for the resulting sprite.

    size

    The size of the sprite in points.

    Return Value

    A newly created and initialized sprite object.

    Import Statement

    Objective-C

    @import SpriteKit;

    Availability

    Available in OS X v10.9 and later.

  • Creates and returns a textured sprite using an image file.

    Declaration

    Objective-C

    + (instancetype)spriteNodeWithImageNamed:(NSString *)name

    Parameters

    name

    The name of an image file stored in the app bundle.

    Return Value

    A newly created and initialized sprite object.

    Import Statement

    Objective-C

    @import SpriteKit;

    Availability

    Available in OS X v10.9 and later.

  • Creates and returns a textured sprite using an existing texture object.

    Declaration

    Objective-C

    + (instancetype)spriteNodeWithTexture:(SKTexture *)texture

    Parameters

    texture

    A Sprite Kit texture.

    Return Value

    A newly created and initialized sprite object.

    Import Statement

    Objective-C

    @import SpriteKit;

    Availability

    Available in OS X v10.9 and later.

  • Initializes a textured sprite using an existing texture object but with a specified size.

    Declaration

    Swift

    convenience init(texture texture: SKTexture!, size size: CGSize)

    Objective-C

    + (instancetype)spriteNodeWithTexture:(SKTexture *)texture size:(CGSize)size

    Parameters

    texture

    A Sprite Kit texture.

    size

    The size of the sprite in points.

    Return Value

    A newly created and initialized sprite object.

    Discussion

    The sprite is initialized using the texture, but the texture’s dimensions are not used. Instead, the size passed into the constructor method is used.

    Import Statement

    Objective-C

    @import SpriteKit;

    Swift

    import SpriteKit

    Availability

    Available in OS X v10.9 and later.

  • Creates and returns a textured sprite using an image file, optionally adding a normal map for lighting.

    Declaration

    Swift

    convenience init(imageNamed name: String, normalMapped generateNormalMap: Bool)

    Objective-C

    + (instancetype)spriteNodeWithImageNamed:(NSString *)name normalMapped:(BOOL)generateNormalMap

    Parameters

    name

    The name of an image file stored in the app bundle.

    generateNormalMap

    If YEStrue, a normal map is generated from the image texture without applying any filter to it (SKTextureNormalMapFilteringTypeNone). If NOfalse, no normal map is generated (matching the behavior of the spriteNodeWithImageNamed: class method).

    Return Value

    A newly created and initialized sprite object.

    Discussion

    The normal map is used only when lighting is enabled in the scene. For more information, see Adding Lighting to a Sprite and SKLightNode Class Reference.

    Import Statement

    Objective-C

    @import SpriteKit;

    Swift

    import SpriteKit

    Availability

    Available in OS X v10.10 and later.

  • Creates and returns a textured sprite with a normal map for lighting.

    Declaration

    Swift

    convenience init(texture texture: SKTexture!, normalMap normalMap: SKTexture?)

    Objective-C

    + (instancetype)spriteNodeWithTexture:(SKTexture *)texture normalMap:(SKTexture *)normalMap

    Parameters

    texture

    A Sprite Kit texture used to draw the sprite.

    normalMap

    A Sprite Kit texture used to add lighting behavior to the sprite.

    Return Value

    A newly created and initialized sprite object.

    Import Statement

    Objective-C

    @import SpriteKit;

    Swift

    import SpriteKit

    Availability

    Available in OS X v10.10 and later.

  • size size Property

    The dimensions of the sprite, in points.

    Declaration

    Swift

    var size: CGSize

    Objective-C

    @property(nonatomic) CGSize size

    Import Statement

    Objective-C

    @import SpriteKit;

    Swift

    import SpriteKit

    Availability

    Available in OS X v10.9 and later.

  • Defines the point in the sprite that corresponds to the node’s position.

    Declaration

    Swift

    var anchorPoint: CGPoint

    Objective-C

    @property(nonatomic) CGPoint anchorPoint

    Discussion

    You specify the value for this property in the unit coordinate space. The default value is (0.5,0.5), which means that the sprite is centered on its position.

    Import Statement

    Objective-C

    @import SpriteKit;

    Swift

    import SpriteKit

    Availability

    Available in OS X v10.9 and later.

  • texture texture Property

    The texture used to draw the sprite.

    Declaration

    Swift

    var texture: SKTexture?

    Objective-C

    @property(nonatomic, retain) SKTexture *texture

    Discussion

    If the value is nil, the sprite is drawn as a colored rectangle using its color property. Otherwise, the texture is used to draw the sprite. The related properties affect how the texture is applied.

    Import Statement

    Objective-C

    @import SpriteKit;

    Swift

    import SpriteKit

    Availability

    Available in OS X v10.9 and later.

  • A property that defines how the texture is applied to the sprite.

    Declaration

    Swift

    var centerRect: CGRect

    Objective-C

    @property(nonatomic) CGRect centerRect

    Discussion

    The rectangle is in the unit coordinate space. The default value is (0,0)-(1.0,1.0), which indicates that the entire texture is stretched to fill the sprite. If a different rectangle is specified, the rectangle’s coordinates are used to break the texture into a 3 x 3 grid. The four corners of this grid are applied without performing any scaling. The upper- and lower-middle parts are scaled horizontally, and the left- and right-middle parts are scaled vertically. The center is scaled in both directions.

    Import Statement

    Objective-C

    @import SpriteKit;

    Swift

    import SpriteKit

    Availability

    Available in OS X v10.9 and later.

  • A floating-point value that describes how the color is blended with the sprite’s texture.

    Declaration

    Swift

    var colorBlendFactor: CGFloat

    Objective-C

    @property(nonatomic) CGFloat colorBlendFactor

    Discussion

    The value must be a number between 0.0 and 1.0, inclusive. The default value (0.0) indicates the color property is ignored and that the texture’s values should be used unmodified. For values greater than 0.0, the texture is blended with the color before being drawn to the scene.

    Import Statement

    Objective-C

    @import SpriteKit;

    Swift

    import SpriteKit

    Availability

    Available in OS X v10.9 and later.

  • color color Property

    The sprite’s color.

    Declaration

    Swift

    var color: NSColor

    Objective-C

    @property(nonatomic, retain) NSColor *color

    Discussion

    If the texture property is non-nil, the red, green, and blue values of the color property are blended with the texture before the texture is drawn and the alpha property is ignored. If the texture property is nil, the color (including the alpha component) is used to draw a colored rectangle.

    Import Statement

    Objective-C

    @import SpriteKit;

    Swift

    import SpriteKit

    Availability

    Available in OS X v10.9 and later.

  • blendMode blendMode Property

    The blend mode used to draw the sprite 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 OS X v10.9 and later.

  • A mask that defines how this sprite is lit by light nodes in the scenes.

    Declaration

    Swift

    var lightingBitMask: UInt32

    Objective-C

    @property(nonatomic) uint32_t lightingBitMask

    Discussion

    To determine whether this sprite is lit by a light node, the sprite’s lightingBitMask property is tested against the light’s categoryBitMask property by performing a logical AND operation. If the comparison results in a nonzero value, the sprite is lit by this light.

    The default value is 0x00000000 (all bits cleared).

    Import Statement

    Objective-C

    @import SpriteKit;

    Swift

    import SpriteKit

    Availability

    Available in OS X v10.10 and later.

  • A mask that defines which lights add additional shadows to the sprite.

    Declaration

    Swift

    var shadowedBitMask: UInt32

    Objective-C

    @property(nonatomic) uint32_t shadowedBitMask

    Discussion

    To determine whether this sprite is affected by being a shadow generated by a light, its shadowCastBitMask property is tested against the light’s categoryBitMask property by performing a logical AND operation. If the comparison results in a nonzero value, the sprite is drawn using a shadowed effect.

    The default value is 0x00000000 (all bits cleared).

    Import Statement

    Objective-C

    @import SpriteKit;

    Swift

    import SpriteKit

    Availability

    Available in OS X v10.10 and later.

  • A mask that defines which lights are occluded by this sprite.

    Declaration

    Swift

    var shadowCastBitMask: UInt32

    Objective-C

    @property(nonatomic) uint32_t shadowCastBitMask

    Discussion

    To determine whether this sprite blocks the light (casting a shadow), the sprite’s shadowedBitMask property is tested against the light’s categoryBitMask property by performing a logical AND operation. If the comparison results in a nonzero value, the sprite casts a shadow past itself.

    Import Statement

    Objective-C

    @import SpriteKit;

    Swift

    import SpriteKit

    Availability

    Available in OS X v10.10 and later.

  • A texture that specifies the normal map for the sprite.

    Declaration

    Swift

    var normalTexture: SKTexture?

    Objective-C

    @property(nonatomic, retain) SKTexture *normalTexture

    Discussion

    A normal map texture is used when a sprite is lit, giving it a more realistic look with shadows and specular highlights. The texture must be a normal map texture.

    Import Statement

    Objective-C

    @import SpriteKit;

    Swift

    import SpriteKit

    Availability

    Available in OS X v10.10 and later.

  • shader shader Property

    A property that determines whether the sprite is rendered using a custom shader.

    Declaration

    Swift

    var shader: SKShader?

    Objective-C

    @property(nonatomic, retain) SKShader *shader

    Discussion

    The default value is nil, which means that the normal behavior for sprite rendering (including any lighting) is performed. If a shader is attached to this property, the custom shader is used to render the sprite.

    Import Statement

    Objective-C

    @import SpriteKit;

    Swift

    import SpriteKit

    Availability

    Available in OS X v10.10 and later.