cocos2d SpriteFrameCache和SpriteBatchNode综合使用

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/zhenyu5211314/article/details/82498589

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);

猜你喜欢

转载自blog.csdn.net/zhenyu5211314/article/details/82498589