SKSpriteNode Class Reference

Inherits from
Conforms to
Framework
/System/Library/Frameworks/SpriteKit.framework
Availability
Available in iOS 7.0 and later.
Companion guide
Declared in
SKSpriteNode.h

Overview

An SKSpriteNode is a node that draws a textured image, a colored square, or a textured image blended with a color.

Tasks

Initializing a New Sprite

Inspecting Physical Properties

Inspecting the Sprite’s Texture

Inspecting Color Properties

Blending the Sprite with the Framebuffer

Properties

anchorPoint

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

@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.

Availability
  • Available in iOS 7.0 and later.
Declared In
SKSpriteNode.h

blendMode

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

@property(nonatomic) SKBlendMode blendMode
Discussion

The default value is SKBlendModeAlpha.

Availability
  • Available in iOS 7.0 and later.
Declared In
SKSpriteNode.h

centerRect

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

@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, then the rectangle’s coordinates are used to break the texture into a 3x3 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.

Availability
  • Available in iOS 7.0 and later.
Declared In
SKSpriteNode.h

color

The sprite’s color.

@property(retain, nonatomic) SKColor *color
Discussion

If the texture property is non-nil, then 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, then the color (including the alpha component) is used to draw a colored rectangle.

Availability
  • Available in iOS 7.0 and later.
Declared In
SKSpriteNode.h

colorBlendFactor

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

@property(nonatomic) CGFloat colorBlendFactor
Discussion

The value must be a number between 0.0 and 1.0, inclusive. The default value (0.0) indicates that 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.

Availability
  • Available in iOS 7.0 and later.
Declared In
SKSpriteNode.h

size

The dimensions of the sprite, in points.

@property(nonatomic) CGSize size
Availability
  • Available in iOS 7.0 and later.
Declared In
SKSpriteNode.h

texture

The texture used to draw the sprite.

@property(retain, nonatomic) SKTexture *texture
Discussion

If the value is nil, then 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.

Availability
  • Available in iOS 7.0 and later.
Declared In
SKSpriteNode.h

Class Methods

spriteNodeWithColor:size:

Creates and returns a colored sprite.

+ (instancetype)spriteNodeWithColor:(SKColor *)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.

Availability
  • Available in iOS 7.0 and later.
Declared In
SKSpriteNode.h

spriteNodeWithImageNamed:

Creates and returns a textured sprite using an image file.

+ (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.

Availability
  • Available in iOS 7.0 and later.
Declared In
SKSpriteNode.h

spriteNodeWithTexture:

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

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

A Sprite Kit texture.

Return Value

A newly created and initialized sprite object.

Availability
  • Available in iOS 7.0 and later.
Declared In
SKSpriteNode.h

spriteNodeWithTexture:size:

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

+ (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.

Availability
  • Available in iOS 7.0 and later.
Declared In
SKSpriteNode.h

Instance Methods

initWithColor:size:

Initializes a colored sprite.

- (instancetype)initWithColor:(SKColor *)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.

Availability
  • Available in iOS 7.0 and later.
Declared In
SKSpriteNode.h

initWithImageNamed:

Initializes a textured sprite using an image file.

- (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).

Availability
  • Available in iOS 7.0 and later.
Declared In
SKSpriteNode.h

initWithTexture:

Initializes a textured sprite using an existing texture object.

- (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).

Availability
  • Available in iOS 7.0 and later.
Declared In
SKSpriteNode.h

initWithTexture:color:size:

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

- (instancetype)initWithTexture:(SKTexture *)texture color:(SKColor *)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.

Availability
  • Available in iOS 7.0 and later.
Declared In
SKSpriteNode.h