精灵sprite的创建方式

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)

CCSpriteBatchNode* batchNode =CCSpriteBatchNode::create("Icon.png", 1000);//参数2指定节点数,默认为29
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);


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值