CCSprite Class Reference
| Inherits from | CCNodeRGBA : CCNode : NSObject |
| Conforms to | CCRGBAProtocol CCTextureProtocol |
| Declared in | CCSprite.h |
Overview
CCSprite is a 2d image ( http://en.wikipedia.org/wiki/Sprite_(computer_graphics) )
CCSprite can be created with an image, or with a sub-rectangle of an image.
If the parent or any of its ancestors is a CCSpriteBatchNode then the following features/limitations are valid
- Features when the parent is a CCBatchNode:
- MUCH faster rendering, specially if the CCSpriteBatchNode has many children. All the children will be drawn in a single batch.
- Limitations
- Camera is not supported yet (eg: CCOrbitCamera action doesn't work)
- GridBase actions are not supported (eg: CCLens, CCRipple, CCTwirl)
- The Alias/Antialias property belongs to CCSpriteBatchNode, so you can't individually set the aliased property.
- The Blending function property belongs to CCSpriteBatchNode, so you can't individually set the blending function property.
- Parallax scroller is not supported, but can be simulated with a "proxy" sprite.
If the parent is an standard CCNode, then CCSprite behaves like any other CCNode: – It supports blending functions – It supports aliasing / antialiasing – But the rendering will be slower: 1 draw per children.
The default anchorPoint in CCSprite is (0.5, 0.5).
extends CCSprite
Tasks
Other Methods
-
dirtywhether or not the Sprite needs to be updated in the Atlas
property -
quadthe quad (tex coords, vertex coords and color) information
property -
atlasIndexThe index used on the TextureAtlas. Don’t modify this value unless you know what you are doing
property -
textureRectreturns the texture rect of the CCSprite in points
property -
textureRectRotatedreturns whether or not the texture rectangle is rotated
property -
flipXwhether or not the sprite is flipped horizontally. It only flips the texture of the sprite, and not the texture of the sprite’s children. Also, flipping the texture doesn’t alter the anchorPoint. If you want to flip the anchorPoint too, and/or to flip the children too use:
property -
flipYwhether or not the sprite is flipped vertically. It only flips the texture of the sprite, and not the texture of the sprite’s children. Also, flipping the texture doesn’t alter the anchorPoint. If you want to flip the anchorPoint too, and/or to flip the children too use:
property -
textureAtlasweak reference of the CCTextureAtlas used when the sprite is rendered using a CCSpriteBatchNode
property -
batchNodeweak reference to the CCSpriteBatchNode that renders the CCSprite
property -
offsetPositionoffset position in points of the sprite in points. Calculated automatically by editors like Zwoptex.
property -
blendFuncconforms to CCTextureProtocol protocol
property -
+ spriteWithTexture:Creates an sprite with a texture. The rect used will be the size of the texture. The offset will be (0,0).
-
+ spriteWithTexture:rect:Creates an sprite with a texture and a rect. The offset will be (0,0).
-
+ spriteWithSpriteFrame:Creates an sprite with an sprite frame.
-
+ spriteWithSpriteFrameName:Creates an sprite with an sprite frame name. An CCSpriteFrame will be fetched from the CCSpriteFrameCache by name. If the CCSpriteFrame doesn’t exist it will raise an exception.
-
+ spriteWithFile:Creates an sprite with an image filename. The rect used will be the size of the image. The offset will be (0,0).
-
+ spriteWithFile:rect:Creates an sprite with an image filename and a rect. The offset will be (0,0).
-
+ spriteWithCGImage:key:Creates an sprite with a CGImageRef and a key. The key is used by the CCTextureCache to know if a texture was already created with this CGImage. For example, a valid key is: @“_spriteframe_01”. If key is nil, then a new texture will be created each time by the CCTextureCache.
-
– initWithTexture:Initializes an sprite with a texture. The rect used will be the size of the texture. The offset will be (0,0).
-
– initWithTexture:rect:Initializes an sprite with a texture and a rect in points (unrotated) The offset will be (0,0).
-
– initWithTexture:rect:rotated:Initializes an sprite with a texture and a rect in points, optionally rotated. The offset will be (0,0). IMPORTANT: This is the designated initializer.
-
– initWithSpriteFrame:Initializes an sprite with an sprite frame.
-
– initWithSpriteFrameName:Initializes an sprite with an sprite frame name. An CCSpriteFrame will be fetched from the CCSpriteFrameCache by name. If the CCSpriteFrame doesn’t exist it will raise an exception.
-
– initWithFile:Initializes an sprite with an image filename. The rect used will be the size of the image. The offset will be (0,0).
-
– initWithFile:rect:Initializes an sprite with an image filename, and a rect. The offset will be (0,0).
-
– initWithCGImage:key:Initializes an sprite with a CGImageRef and a key The key is used by the CCTextureCache to know if a texture was already created with this CGImage. For example, a valid key is: @“_spriteframe_01”. If key is nil, then a new texture will be created each time by the CCTextureCache.
-
– updateTransformupdates the quad according the the rotation, position, scale values.
-
– setTextureRect:set the texture rect of the CCSprite in points. It will call setTextureRect:rotated:untrimmedSize with rotated = NO, and utrimmedSize = rect.size.
-
– 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.
-
– setVertexRect:set the vertex rect. It will be called internally by setTextureRect. Useful if you want to create 2x images from SD images in Retina Display. Do not call it manually. Use setTextureRect instead.
-
– setDisplayFrame:sets a new display frame to the CCSprite.
-
– isFrameDisplayed:returns whether or not a CCSpriteFrame is being displayed
-
– displayFramereturns the current displayed frame.
-
– setDisplayFrameWithAnimationName:index:changes the display frame with animation name and index. The animation name will be get from the CCAnimationCache
Other Methods
-
opacityopacity: conforms to CCRGBAProtocol protocol
property -
colorRGB colors: conforms to CCRGBAProtocol protocol
property -
usesSpriteSheetwhether or not the Sprite is rendered using a CCSpriteSheet
property -
spriteSheetweak reference to the CCSpriteSheet that renders the CCSprite
property -
honorParentTransformwhether or not to transform according to its parent transfomrations. Useful for health bars. eg: Don’t rotate the health bar, even if the parent rotates. IMPORTANT: Only valid if it is rendered using an CCSpriteSheet.
property -
+ spriteWithTexture:rect:offset:Creates an sprite with a texture, a rect and offset.
-
+ spriteWithCGImage:Creates an sprite with a CGImageRef. (Deprecated: Use spriteWithCGImage:key: instead. Will be removed in v1.0 final)
-
+ spriteWithSpriteSheet:rect:Creates an sprite with an CCSpriteSheet and a rect
-
– initWithCGImage:Initializes an sprite with a CGImageRef (Deprecated: Use spriteWithCGImage:key: instead. Will be removed in v1.0 final)
-
– initWithSpriteSheet:rect:Initializes an sprite with an CCSpriteSheet and a rect
-
– useSelfRendertell the sprite to use self-render.
-
– useSpriteSheetRender:tell the sprite to use sprite sheet render.
-
– displayedFramereturns the current displayed frame.
-
– setDisplayFrame:index:changes the display frame based on an animation and an index.
-
– animationByName:returns an Animation given it’s name.
-
– addAnimation:adds an Animation to the Sprite.
KoboldExtensions Methods
-
– playAnimWithFormat:numFrames:firstIndex:delay:animateTag:Plays an anim once with the given file format (eg @“playeranim%i.png”) with the given number of frames and a starting index. The animateTag parameter is the tag given to the action playing the animation, so that you can stop it by tag.
-
– playAnimLoopedWithFormat:numFrames:firstIndex:delay:animateTag:Plays an anim looped with the given file format (eg @“playeranim%i.png”) with the given number of frames and a starting index. The animateTag parameter is the tag given to the action playing the animation, so that you can stop it by tag.
-
– playAnimAndRemoveWithFormat:numFrames:firstIndex:delay:animateTag:Plays an anim once with the given file format (eg @“playeranim%i.png”) with the given number of frames and a starting index. The animateTag parameter is the tag given to the action playing the animation, so that you can stop it by tag. When the anim has cycled through all frames, the node running the animation will be removed from its parent. Useful for one-off animations, like an explosion, smoke-puff, blood splat, etc.
-
– playAnimWithFormat:numFrames:firstIndex:delay:animateTag:restoreOriginalFrame:Variant that allows to set the restoreOriginalFrame property.
-
– playAnimLoopedWithFormat:numFrames:firstIndex:delay:animateTag:restoreOriginalFrame:Variant that allows to set the restoreOriginalFrame property.
-
– playAnimAndRemoveWithFormat:numFrames:firstIndex:delay:animateTag:restoreOriginalFrame:Variant that allows to set the restoreOriginalFrame property.
-
+ spriteWithSpriteFrameNameOrFile:First, checks if the string can be found in the spriteFrameCache, if not it will try to load the string assuming its a file name.
-
+ spriteWithRenderTexture:Creates an autoreleased sprite from a CCRenderTexture
Deprecated Methods
Properties
atlasIndex
The index used on the TextureAtlas. Don’t modify this value unless you know what you are doing
@property (nonatomic, readwrite) NSUInteger atlasIndexDeclared In
CCSprite.hbatchNode
weak reference to the CCSpriteBatchNode that renders the CCSprite
@property (nonatomic, readwrite, assign) CCSpriteBatchNode *batchNodeDeclared In
CCSprite.hblendFunc
conforms to CCTextureProtocol protocol
@property (nonatomic, readwrite) ccBlendFunc blendFuncDeclared In
CCSprite.hcolor
RGB colors: conforms to CCRGBAProtocol protocol
@property (nonatomic, readwrite) ccColor3B colorDeclared In
CCSprite.hdirty
whether or not the Sprite needs to be updated in the Atlas
@property (nonatomic, readwrite) BOOL dirtyDeclared In
CCSprite.hflipX
whether or not the sprite is flipped horizontally. It only flips the texture of the sprite, and not the texture of the sprite’s children. Also, flipping the texture doesn’t alter the anchorPoint. If you want to flip the anchorPoint too, and/or to flip the children too use:
@property (nonatomic, readwrite) BOOL flipXDiscussion
sprite.scaleX *= -1;
Declared In
CCSprite.hflipY
whether or not the sprite is flipped vertically. It only flips the texture of the sprite, and not the texture of the sprite’s children. Also, flipping the texture doesn’t alter the anchorPoint. If you want to flip the anchorPoint too, and/or to flip the children too use:
@property (nonatomic, readwrite) BOOL flipYDiscussion
sprite.scaleY *= -1;
Declared In
CCSprite.hhonorParentTransform
whether or not to transform according to its parent transfomrations. Useful for health bars. eg: Don’t rotate the health bar, even if the parent rotates. IMPORTANT: Only valid if it is rendered using an CCSpriteSheet.
@property (nonatomic, readwrite) ccHonorParentTransform honorParentTransformAvailability
Declared In
CCSprite.hoffsetPosition
offset position in points of the sprite in points. Calculated automatically by editors like Zwoptex.
@property (nonatomic, readonly) CGPoint offsetPositionAvailability
Declared In
CCSprite.hopacity
opacity: conforms to CCRGBAProtocol protocol
@property (nonatomic, readwrite) GLubyte opacityDeclared In
CCSprite.hquad
the quad (tex coords, vertex coords and color) information
@property (nonatomic, readonly) ccV3F_C4B_T2F_Quad quadDeclared In
CCSprite.hspriteSheet
weak reference to the CCSpriteSheet that renders the CCSprite
@property (nonatomic, readwrite, assign) CCSpriteSheet *spriteSheetDeclared In
CCSprite.htextureAtlas
weak reference of the CCTextureAtlas used when the sprite is rendered using a CCSpriteBatchNode
@property (nonatomic, readwrite, assign) CCTextureAtlas *textureAtlasDeclared In
CCSprite.htextureRect
returns the texture rect of the CCSprite in points
@property (nonatomic, readonly) CGRect textureRectDeclared In
CCSprite.hClass Methods
spriteWithCGImage:
Creates an sprite with a CGImageRef. (Deprecated: Use spriteWithCGImage:key: instead. Will be removed in v1.0 final)
+ (id)spriteWithCGImage:(CGImageRef)imageDeclared In
CCSprite.hspriteWithCGImage:key:
Creates an sprite with a CGImageRef and a key. The key is used by the CCTextureCache to know if a texture was already created with this CGImage. For example, a valid key is: @“_spriteframe_01”. If key is nil, then a new texture will be created each time by the CCTextureCache.
+ (id)spriteWithCGImage:(CGImageRef)image key:(NSString *)keyAvailability
Declared In
CCSprite.hspriteWithFile:
Creates an sprite with an image filename. The rect used will be the size of the image. The offset will be (0,0).
+ (id)spriteWithFile:(NSString *)filenameDeclared In
CCSprite.hspriteWithFile:rect:
Creates an sprite with an image filename and a rect. The offset will be (0,0).
+ (id)spriteWithFile:(NSString *)filename rect:(CGRect)rectDeclared In
CCSprite.hspriteWithRenderTexture:
Creates an autoreleased sprite from a CCRenderTexture
+ (id)spriteWithRenderTexture:(CCRenderTexture *)rtxDeclared In
CCSpriteExtensions.hspriteWithSpriteFrame:
Creates an sprite with an sprite frame.
+ (id)spriteWithSpriteFrame:(CCSpriteFrame *)spriteFrameDeclared In
CCSprite.hspriteWithSpriteFrameName:
Creates an sprite with an sprite frame name. An CCSpriteFrame will be fetched from the CCSpriteFrameCache by name. If the CCSpriteFrame doesn’t exist it will raise an exception.
+ (id)spriteWithSpriteFrameName:(NSString *)spriteFrameNameAvailability
Declared In
CCSprite.hspriteWithSpriteFrameNameOrFile:
First, checks if the string can be found in the spriteFrameCache, if not it will try to load the string assuming its a file name.
+ (id)spriteWithSpriteFrameNameOrFile:(NSString *)nameOrFileDeclared In
CCSpriteExtensions.hspriteWithSpriteSheet:rect:
Creates an sprite with an CCSpriteSheet and a rect
+ (id)spriteWithSpriteSheet:(CCSpriteSheet *)spritesheet rect:(CGRect)rectDeclared In
CCSprite.hspriteWithTexture:
Creates an sprite with a texture. The rect used will be the size of the texture. The offset will be (0,0).
+ (id)spriteWithTexture:(CCTexture2D *)textureDeclared In
CCSprite.hInstance Methods
addAnimation:
adds an Animation to the Sprite.
- (void)addAnimation:(CCAnimation *)animationDeclared In
CCSprite.hanimationByName:
returns an Animation given it’s name.
- (CCAnimation *)animationByName:(NSString *)animationNameDeclared In
CCSprite.hdisplayFrame
returns the current displayed frame.
- (CCSpriteFrame *)displayFrameDeclared In
CCSprite.hdisplayedFrame
returns the current displayed frame.
- (CCSpriteFrame *)displayedFrameDeclared In
CCSprite.hinitWithCGImage:
Initializes an sprite with a CGImageRef (Deprecated: Use spriteWithCGImage:key: instead. Will be removed in v1.0 final)
- (id)initWithCGImage:(CGImageRef)imageDeclared In
CCSprite.hinitWithCGImage:key:
Initializes an sprite with a CGImageRef and a key The key is used by the CCTextureCache to know if a texture was already created with this CGImage. For example, a valid key is: @“_spriteframe_01”. If key is nil, then a new texture will be created each time by the CCTextureCache.
- (id)initWithCGImage:(CGImageRef)image key:(NSString *)keyAvailability
Declared In
CCSprite.hinitWithFile:
Initializes an sprite with an image filename. The rect used will be the size of the image. The offset will be (0,0).
- (id)initWithFile:(NSString *)filenameDeclared In
CCSprite.hinitWithFile:rect:
Initializes an sprite with an image filename, and a rect. The offset will be (0,0).
- (id)initWithFile:(NSString *)filename rect:(CGRect)rectDeclared In
CCSprite.hinitWithSpriteFrame:
Initializes an sprite with an sprite frame.
- (id)initWithSpriteFrame:(CCSpriteFrame *)spriteFrameDeclared In
CCSprite.hinitWithSpriteFrameName:
Initializes an sprite with an sprite frame name. An CCSpriteFrame will be fetched from the CCSpriteFrameCache by name. If the CCSpriteFrame doesn’t exist it will raise an exception.
- (id)initWithSpriteFrameName:(NSString *)spriteFrameNameAvailability
Declared In
CCSprite.hinitWithSpriteSheet:rect:
Initializes an sprite with an CCSpriteSheet and a rect
- (id)initWithSpriteSheet:(CCSpriteSheet *)spritesheet rect:(CGRect)rectDeclared In
CCSprite.hinitWithTexture:
Initializes an sprite with a texture. The rect used will be the size of the texture. The offset will be (0,0).
- (id)initWithTexture:(CCTexture2D *)textureDeclared In
CCSprite.hinitWithTexture:rect:
Initializes an sprite with a texture and a rect in points (unrotated) The offset will be (0,0).
- (id)initWithTexture:(CCTexture2D *)texture rect:(CGRect)rectDeclared In
CCSprite.hinitWithTexture:rect:rotated:
Initializes an sprite with a texture and a rect in points, optionally rotated. The offset will be (0,0). IMPORTANT: This is the designated initializer.
- (id)initWithTexture:(CCTexture2D *)texture rect:(CGRect)rect rotated:(BOOL)rotatedDeclared In
CCSprite.hisFrameDisplayed:
returns whether or not a CCSpriteFrame is being displayed
- (BOOL)isFrameDisplayed:(CCSpriteFrame *)frameDeclared In
CCSprite.hplayAnimAndRemoveWithFormat:numFrames:firstIndex:delay:animateTag:
Plays an anim once with the given file format (eg @“playeranim%i.png”) with the given number of frames and a starting index. The animateTag parameter is the tag given to the action playing the animation, so that you can stop it by tag. When the anim has cycled through all frames, the node running the animation will be removed from its parent. Useful for one-off animations, like an explosion, smoke-puff, blood splat, etc.
- (void)playAnimAndRemoveWithFormat:(NSString *)format numFrames:(int)numFrames firstIndex:(int)firstIndex delay:(float)delay animateTag:(int)animateTagDeclared In
CCSpriteExtensions.hplayAnimAndRemoveWithFormat:numFrames:firstIndex:delay:animateTag:restoreOriginalFrame:
Variant that allows to set the restoreOriginalFrame property.
- (void)playAnimAndRemoveWithFormat:(NSString *)format numFrames:(int)numFrames firstIndex:(int)firstIndex delay:(float)delay animateTag:(int)animateTag restoreOriginalFrame:(BOOL)restoreOriginalFrameDeclared In
CCSpriteExtensions.hplayAnimLoopedWithFormat:numFrames:firstIndex:delay:animateTag:
Plays an anim looped with the given file format (eg @“playeranim%i.png”) with the given number of frames and a starting index. The animateTag parameter is the tag given to the action playing the animation, so that you can stop it by tag.
- (void)playAnimLoopedWithFormat:(NSString *)format numFrames:(int)numFrames firstIndex:(int)firstIndex delay:(float)delay animateTag:(int)animateTagDeclared In
CCSpriteExtensions.hplayAnimLoopedWithFormat:numFrames:firstIndex:delay:animateTag:restoreOriginalFrame:
Variant that allows to set the restoreOriginalFrame property.
- (void)playAnimLoopedWithFormat:(NSString *)format numFrames:(int)numFrames firstIndex:(int)firstIndex delay:(float)delay animateTag:(int)animateTag restoreOriginalFrame:(BOOL)restoreOriginalFrameDeclared In
CCSpriteExtensions.hplayAnimWithFormat:numFrames:firstIndex:delay:animateTag:
Plays an anim once with the given file format (eg @“playeranim%i.png”) with the given number of frames and a starting index. The animateTag parameter is the tag given to the action playing the animation, so that you can stop it by tag.
- (void)playAnimWithFormat:(NSString *)format numFrames:(int)numFrames firstIndex:(int)firstIndex delay:(float)delay animateTag:(int)animateTagDeclared In
CCSpriteExtensions.hplayAnimWithFormat:numFrames:firstIndex:delay:animateTag:restoreOriginalFrame:
Variant that allows to set the restoreOriginalFrame property.
- (void)playAnimWithFormat:(NSString *)format numFrames:(int)numFrames firstIndex:(int)firstIndex delay:(float)delay animateTag:(int)animateTag restoreOriginalFrame:(BOOL)restoreOriginalFrameDeclared In
CCSpriteExtensions.hsetDisplayFrame:
sets a new display frame to the CCSprite.
- (void)setDisplayFrame:(CCSpriteFrame *)newFrameDeclared In
CCSprite.hsetDisplayFrame:index:
changes the display frame based on an animation and an index.
- (void)setDisplayFrame:(NSString *)animationName index:(int)frameIndexDeclared In
CCSprite.hsetDisplayFrameWithAnimationName:index:
changes the display frame with animation name and index. The animation name will be get from the CCAnimationCache
- (void)setDisplayFrameWithAnimationName:(NSString *)animationName index:(int)frameIndexAvailability
Declared In
CCSprite.hsetTextureRect:
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)rectDeclared In
CCSprite.hsetTextureRect: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)sizeDeclared In
CCSprite.hsetVertexRect:
set the vertex rect. It will be called internally by setTextureRect. Useful if you want to create 2x images from SD images in Retina Display. Do not call it manually. Use setTextureRect instead.
- (void)setVertexRect:(CGRect)rectDeclared In
CCSprite.hupdateTransform
updates the quad according the the rotation, position, scale values.
- (void)updateTransformDeclared In
CCSprite.h