Class Detail

cc.Sprite(fileName, rect, rotated)

cc.Sprite is a 2d image ( http://en.wikipedia.org/wiki/Sprite_(computer_graphics) )
cc.Sprite can be created with an image, or with a sub-rectangle of an image.
If the parent or any of its ancestors is a cc.SpriteBatchNode then the following features/limitations are valid
- Features when the parent is a cc.BatchNode:
- MUCH faster rendering, specially if the cc.SpriteBatchNode 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 cc.Node, then cc.Sprite behaves like any other cc.Node:
- It supports blending functions
- It supports aliasing / antialiasing
- But the rendering will be slower: 1 draw per children.
The default anchorPoint in cc.Sprite is (0.5, 0.5).

1.Create a sprite with image path and rect
var sprite1 = new cc.Sprite("res/HelloHTML5World.png");
var sprite2 = new cc.Sprite("res/HelloHTML5World.png",cc.rect(0,0,480,320));
2.Create a sprite with a sprite frame name. Must add "#" before frame name.
var sprite = new cc.Sprite('#grossini_dance_01.png');
3.Create a sprite with a sprite frame
var spriteFrame = cc.spriteFrameCache.getSpriteFrame("grossini_dance_01.png");
var sprite = new cc.Sprite(spriteFrame);
4.Create a sprite with an existing texture contained in a CCTexture2D object
After creation, the rect will be the size of the texture, and the offset will be (0,0).
var texture = cc.textureCache.addImage("HelloHTML5World.png");
var sprite1 = new cc.Sprite(texture);
var sprite2 = new cc.Sprite(texture, cc.rect(0,0,480,320));

Initializes an empty sprite with nothing init.
Please pass parameters to the constructor to initialize the sprite, do not call this function yourself.

Returns:

{Boolean}

{Boolean}initWithFile(filename, rect)

Initializes a sprite with an image filename.
This method will find pszFilename from local file system, load its content to CCTexture2D,
then use CCTexture2D to create a sprite.
After initialization, the rect used will be the size of the image. The offset will be (0,0).
Please pass parameters to the constructor to initialize the sprite, do not call this function yourself.

{Boolean} true if the sprite is initialized properly, false otherwise.

{Boolean}initWithSpriteFrame(spriteFrame)

Initializes a sprite with a SpriteFrame. The texture and rect in SpriteFrame will be applied on this sprite.
Please pass parameters to the constructor to initialize the sprite, do not call this function yourself,

{Boolean} true if the sprite is initialized properly, false otherwise.

{Boolean}initWithSpriteFrameName(spriteFrameName)

Initializes a sprite with a sprite frame name.
A cc.SpriteFrame will be fetched from the cc.SpriteFrameCache by name.
If the cc.SpriteFrame doesn't exist it will raise an exception.
Please pass parameters to the constructor to initialize the sprite, do not call this function yourself.

A key string that can fected a valid cc.SpriteFrame from cc.SpriteFrameCache

Returns:

{Boolean} true if the sprite is initialized properly, false otherwise.

{Boolean}initWithTexture(texture, rect, rotated, counterclockwise)

Initializes a sprite with a texture and a rect in points, optionally rotated.
After initialization, the rect used will be the size of the texture, and the offset will be (0,0).
Please pass parameters to the constructor to initialize the sprite, do not call this function yourself.

Only the contents inside rect of this texture will be applied for this sprite.

{Boolean}rotatedOptional

Whether or not the texture rectangle is rotated.

{Boolean}counterclockwiseOptional, Default: true

Whether or not the texture rectangle rotation is counterclockwise (texture package is counterclockwise, spine is clockwise).

Returns:

{Boolean} true if the sprite is initialized properly, false otherwise.

{Boolean}isDirty()

Returns whether or not the Sprite needs to be updated in the Atlas

Returns:

{Boolean} True if the sprite needs to be updated in the Atlas, false otherwise.

{Boolean}isFlippedX()

Returns the flag which indicates whether the sprite is flipped horizontally or not.

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:
sprite.setScaleX(sprite.getScaleX() * -1);

Returns:

{Boolean} true if the sprite is flipped horizontally, false otherwise.

{Boolean}isFlippedY()

Return the flag which indicates whether the sprite is flipped vertically or not.

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:
sprite.setScaleY(sprite.getScaleY() * -1);

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.
(override this method to generate "double scale" sprites)