cocos study notes-Sprite
Reference: [jellythink](“http://www.jellythink.com/archives/742“)
1.Sprite class
Sprite
is a 2D picture object,which can be defined by using a picture or part of picture.A Sprite
can move, rotate, play action and so on. It consist of Texture
,Frame
,Animation
.
important:
We usually use Texture2D to load the picture, then use it to produce SpriteFrame
.After that , we add SpriteFrame
to the Animation
to produce action data.Finally, Animation
produces Animate
.Then Sprite
puts it into action.
2.Sprite
API:
//create an empty sprite and then use `setTexture` to set the texture
static Sprite* create();
//create a sprite with an image
static Sprite* create(const std::string& filename);
//create a sprite with a Texture2D object
static Sprite* createWithTexture(Texture2D* texture);
//create a sprite by using spriteframe
static Sprite* createWithSpriteFrame(SpriteFrame *spriteFrame);
/*create a sprite by using a spriteframe,inside the programe, IDE will get `SpriteFrame` from `SpriteFrameCache` according to `SpriteFrameName` parameter */
static Sprite* createWithSpriteFrameName(const std::string& spriteFrameName)
3.SpriteBatchNode
If we need to render many sprites,we can use SpriteBatchNode
to render them in one time to save spending,like this:
auto batchNode = SpriteBatchNode::create("CloseNormal.png", 1000);
batchNode->setPosition(Point::ZERO);
this->addChild(batchNode);
for (int i = 0;i < 1000;i++)
{
int x = rand() % 960;
int y = rand() % 640;
auto testIcon = Sprite::createWithTexture(batchNode->getTexture());
testIcon->setPosition(ccp(x, y));
batchNode->addChild(testIcon);
}