原文: CCSpriteFrameCache的用法
先用zwoptexapp.com制作文件AnimBear.plist和AnimBear.png
cocos2d-x代码:
CCSpriteFrameCache::sharedSpriteFrameCache()->addSpriteFramesWithFile("AnimBear.plist");
CCSpriteBatchNode* spriteSheet = CCSpriteBatchNode::batchNodeWithFile("AnimBear.png");
this->addChild(spriteSheet);
CCArray *walkAnimFrames = new CCArray();
for(int i = 1; i <= 8; ++i) {
walkAnimFrames->addObject(
CCSpriteFrameCache::sharedSpriteFrameCache()->spriteFrameByName(
CCString::stringWithFormat("bear%d.png", i)->getCString()
)
);
}
CCAnimation *walkAnim = CCAnimation::animationWithSpriteFrames(walkAnimFrames, 0.1f);
CCSize winSize = CCDirector::sharedDirector()->getWinSize();
CCSprite* bear = CCSprite::spriteWithSpriteFrameName("bear1.png");
bear->setPosition(ccp(winSize.width/2, winSize.height/2));
CCRepeatForever* walkAction = CCRepeatForever::actionWithAction(
CCAnimate::actionWithAnimation(walkAnim)
);
bear->runAction(walkAction);
this->addChild(bear);