cocos2d SpriteFrameCache和SpriteBatchNode综合使用

SpriteFrameCache:用于快速加载,一次加载一整张图片

cc.SpriteFrameCache.getInstance().addSpriteFrames("res/ui/worldBoss/worldbossmap.plist");
var spriteFrame = cc.SpriteFrameCache.getInstance().getSpriteFrame(cellData.imgage+".png");
if (spriteFrame) {
    cellImg.setSpriteFrame(spriteFrame);
} else {
    cellImg.setTexture("res/ui/Alliance/"+cellData.imgage+".png");
}
// 或者
var cellImg = cc.Sprite.createWithSpriteFrameName(cellData.imgage+".png");

SpriteBatchNode:用于减少drawcall,减少卡顿,相同图片drawcall一次,生成的sprite必须加到这个batchnode上,且添加到同一个CCSpriteBatchNode中的CCSprite必须使用同一个纹理图片,所以需要打包所有图片,和SpriteFrameCache结合使用

var batch= cc.SpriteBatchNode.create("Icon-114.png");
this.addChild(batch);
var spriteA = cc.Sprite.create(batch->getTexture());
batch.addChild(spriteA);

SpriteFrameCache 和 SpriteBatchNode结合使用:A.plist和A.png为同一texturePackers导出资源,且包含所有加到batchnode上的图片,默认子节点的数量是29(数量不够时,系统会自己增加)

CCSpriteFrameCache::sharedSpriteFrameCache()->addSpriteFramesWithFile("A.plist");
CCSpriteBatchNode* batchNode = CCSpriteBatchNode::create("A.png", 50);

CCSprite* sprite1 = CCSprite::createWithSpriteFrameName("AChildOne.png");
sprite1.setTexture(batchNode.getTexture());
CCSprite* sprite2 = CCSprite::createWithSpriteFrameName("AChildTwo.png");
sprite2.setTexture(batchNode.getTexture());

batchNode->addChild(sprite1);
batchNode->addChild(sprite2);
this->addChild(batchNode);
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值