1、通过fileImage直接创建
(1)CCSprite* sprite = CCSprite::create(const char *pszFileName);
(2)CCSprite* sprite = CCSprite::create(const char *pszFileName, const CCRect& rect);//参数1为图片名称,参数2为指定的区域
2、通过CCTexture2D创建
(1)
CCTexture2D* texture = CCTextureCache::sharedTextureCache()->addImage("XX.png");
CCSprite* sprite = CCSprite::createWithTexture(CCTexture2D* texture);
(2)
CCTexture2D* texture = CCTextureCache::sharedTextureCache()->addImage("XX.png");
CCSprite* sprite = CCSprite::createWithTexture(CCTexture2D* texture,const CCRect& rect);
3、通过CCSpriteBatchNode创建(能够降低图片渲染的批次,提高效率)
(1)
batchNode->setPosition(CCPointZero);
this->addChild(batchNode);
for(int i = 0;i < 1000;++i){
int x = arc4random();
int y = arc4random();
CCSprite* testIcon =CCSprite::createWithTexture(batchNode->getTexture());
testIcon->setPosition( ccp(x, y));
batchNode->addChild(testIcon);
}
注意:加入到CCSpriteBatchNode中的sprite必须来自同一张纹理。
4、通过CCSpriteFrameCache创建
(1)
CCSpriteFrameCache* cache = CCSpriteFrameCache::sharedSpriteFrameCache();
cache->addSpriteFramesWithFile("animations/grossini.plist", "animations/grossini.png");
CCSprite *sprite = CCSprite::spriteWithSpriteFrameName("father.gif"); //"father.gif"为拼接到"grossini.png"中的一部分
5、通过CCSpriteBatchNode和CCSpriteFrameCache结合创建
前提:必须保证CCSpriteFrameCache和CCSpriteBatchNode加载的是同一纹理贴图
(1)
CCSpriteFrameCache::sharedSpriteFrameCache()->addSpriteFramesWithFile("animations/ghosts.plist", "animations/ghosts.png");
CCSpriteBatchNode *aParent = CCSpriteBatchNode::batchNodeWithFile("animations/ghosts.png");
addChild(aParent, 0, kTagSprite1);
CCSprite *pFather = CCSprite::spriteWithSpriteFrameName("father.gif");
pFather->setPosition(ccp( s.width/2, s.height/2));
aParent->addChild(pFather, 0, kTagSprite2);