CCSprite Class Reference

Inherits from CCNode : CCResponder : NSObject
Conforms to #
CCBlendProtocol
CCEffectProtocol
CCShaderProtocol
CCTextureProtocol
CC_EFFECTS
endif
if
Declared in CCSprite.h

Overview

CCSprite draws a CCTexture on the screen. CCSprite can be created with an image, with a sub-rectangle of an (atlas) image.

The default anchorPoint in CCSprite is (0.5, 0.5).

Creating a Sprite with an Image File or Sprite Frame Name

+ spriteWithImageNamed:

Creates a sprite with the name of an image. The name can be either a name in a sprite sheet or the name of a file.

+ (id)spriteWithImageNamed:(NSString *)imageName

Parameters

imageName

name of the image to load.

Return Value

The CCSprite Object.

Declared In

CCSprite.h

– initWithImageNamed:

Initializes a sprite with the name of an image. The name can be either a name in a sprite sheet or the name of a file.

- (id)initWithImageNamed:(NSString *)imageName

Parameters

imageName

name of the image to load.

Return Value

A newly initialized CCSprite object.

Declared In

CCSprite.h

Creating a Sprite with a Sprite Frame

+ spriteWithSpriteFrame:

Creates a sprite with an existing CCSpriteFrame.

+ (id)spriteWithSpriteFrame:(CCSpriteFrame *)spriteFrame

Parameters

spriteFrame

Sprite frame to use.

Return Value

The CCSprite Object.

See Also

Declared In

CCSprite.h

– initWithSpriteFrame:

Initializes an sprite with an existing CCSpriteFrame.

- (id)initWithSpriteFrame:(CCSpriteFrame *)spriteFrame

Parameters

spriteFrame

Sprite frame to use.

Return Value

A newly initialized CCSprite object.

See Also

Declared In

CCSprite.h

Creating a Sprite with a Texture

+ spriteWithTexture:

Creates a sprite with an existing CCTexture. The rect used will be the size of the texture. The offset will be (0,0).

+ (id)spriteWithTexture:(CCTexture *)texture

Parameters

texture

Texture to use.

Return Value

The CCSprite Object.

See Also

Declared In

CCSprite.h

+ spriteWithTexture:rect:

Creates a sprite with an existing CCTexture. The offset will be (0,0).

+ (id)spriteWithTexture:(CCTexture *)texture rect:(CGRect)rect

Parameters

texture

Texture to use.

rect

Rect to use.

Return Value

The CCSprite Object.

See Also

Declared In

CCSprite.h

– initWithTexture:

Initializes a sprite with an existing CCTexture. The rect used will be the size of the texture. The offset will be (0,0).

- (id)initWithTexture:(CCTexture *)texture

Parameters

texture

The texture to use.

Return Value

A newly initialized CCSprite object.

See Also

Declared In

CCSprite.h

– initWithTexture:rect:

Initializes a sprite with an existing CCTexture and a rect in points (unrotated). The offset will be (0,0).

- (id)initWithTexture:(CCTexture *)texture rect:(CGRect)rect

Parameters

texture

The texture to use.

rect

The rect to use.

Return Value

A newly initialized CCSprite object.

See Also

Declared In

CCSprite.h

– initWithTexture:rect:rotated:

Initializes a sprite with an existing CCTexture and a rect in points, optionally rotated. The offset will be (0,0).

- (id)initWithTexture:(CCTexture *)texture rect:(CGRect)rect rotated:(BOOL)rotated

Parameters

texture

The texture to use.

rect

The rect to use.

rotated

YES if texture is rotated.

Return Value

A newly initialized CCSprite object.

Discussion

Note: This is the designated initializer.

See Also

Declared In

CCSprite.h

Creating a Sprite with a CGImage

+ spriteWithCGImage:key:

Creates an sprite with a CGImageRef and a key. The key is used to determine if a texture was already created with this CGImage, this ensures proper caching. For example, a valid key is: @“_spriteframe_01”.

+ (id)spriteWithCGImage:(CGImageRef)image key:(NSString *)key

Parameters

image

Image ref.

key

Key description.

Return Value

The CCSprite Object.

Discussion

Warning: If key is nil, then a new texture will be created each time rather than replacing an existing texture using the same key. This can waste a lot of memory!

Declared In

CCSprite.h

– initWithCGImage:key:

Initializes an sprite with a CGImageRef and a key. The key is used to determine if a texture was already created with this CGImage, this ensures proper caching. For example, a valid key is: @“_spriteframe_01”.

- (id)initWithCGImage:(CGImageRef)image key:(NSString *)key

Parameters

image

Image ref.

key

Key description.

Return Value

A newly initialized CCSprite object.

Discussion

Warning: If key is nil, then a new texture will be created each time rather than replacing an existing texture using the same key. This can waste a lot of memory!

Declared In

CCSprite.h

Creating an empty Sprite

+ emptySprite

Creates an “empty” (invisible) sprite. The primary use of this type of sprite would be for adding control sprites for more complex animations.

+ (id)emptySprite

Return Value

A newly initialized CCSprite object.

Declared In

CCSprite.h

Flipping a Sprite

  flipX

Whether or not the sprite is flipped horizontally.

@property (nonatomic, readwrite) BOOL flipX

Discussion

Note: Flipping does not flip any of the sprite’s child sprites nor does it alter the anchorPoint. If that is what you want, you should try inversing the CCNode scaleX property: sprite.scaleX *= -1.0;.

Declared In

CCSprite.h

  flipY

Whether or not the sprite is flipped vertically.

@property (nonatomic, readwrite) BOOL flipY

Discussion

Note: Flipping does not flip any of the sprite’s child sprites nor does it alter the anchorPoint. If that is what you want, you should try inversing the CCNode scaleY property: sprite.scaleY *= -1.0;.

Declared In

CCSprite.h

Accessing the Sprite Frames

  spriteFrame

The currently displayed spriteFrame.

@property (nonatomic, strong) CCSpriteFrame *spriteFrame

See Also

Declared In

CCSprite.h

  normalMapSpriteFrame

The current normal map spriteFrame.

@property (nonatomic, strong) CCSpriteFrame *normalMapSpriteFrame

See Also

Declared In

CCSprite.h

Working with the Sprite's Texture

  offsetPosition

The offset position in points of the sprite in points. Calculated automatically by sprite sheet editors.

@property (nonatomic, readonly) CGPoint offsetPosition

Declared In

CCSprite.h

  textureRect

Returns the texture rect of the CCSprite in points.

@property (nonatomic, readonly) CGRect textureRect

Declared In

CCSprite.h

  textureRectRotated

Returns whether or not the texture rectangle is rotated. Sprite sheet editors may rotate sprite frames in a texture to fit more sprites in the same atlas.

@property (nonatomic, readonly) BOOL textureRectRotated

Declared In

CCSprite.h

– setTextureRect:

Set the texture rect of the CCSprite in points. It will call setTextureRect:rotated:untrimmedSize with rotated = NO, and utrimmedSize = rect.size.

- (void)setTextureRect:(CGRect)rect

Parameters

rect

Rect to use.

Declared In

CCSprite.h

– setTextureRect:rotated:untrimmedSize:

Set the texture rect, rectRotated and untrimmed size of the CCSprite in points. It will update the texture coordinates and the vertex rectangle.

- (void)setTextureRect:(CGRect)rect rotated:(BOOL)rotated untrimmedSize:(CGSize)size

Parameters

rect

Rect to use.

rotated

YES if texture is rotated.

size

Untrimmed size.

Declared In

CCSprite.h

– nodeToTextureTransform

Returns the matrix that transforms the sprite’s (local) space coordinates into the sprite’s texture space coordinates.

- (CGAffineTransform)nodeToTextureTransform

Declared In

CCSprite.h