照着教程扒代碼,,發現版本不一樣,函數有略微差別。
折騰完之後來記錄一下。
版本:cocos2d-2.1rc0-x-2.1.2
1.建立紋理
CCTexture2D *pTexture = CCTextureCache::sharedTextureCache()->addImage("xxx.yyy");
2.用紋理構造每一幀
CCSpriteFame *frame0 = CCSpriteFrame::createWithTexture(pTexture, CCRectMake(x, y, width, height));
.........
CCSpriteFame *framen = CCSpriteFrame::createWithTexture(pTexture, CCRectMake(x, y, width, height));
3.將幀封裝成CCArray
CCArray *pArray = CCArray::create();
pArray->addObject(frame0);
.........
pArray->addObject(framen);
4.用CCArray構造CCAnimation
CCAnimation *animation = CCAnimation::createWithSpriteFrames(pArray, xx.xf);
CC_BREAK_IF(!animation);
5.初始化一個精靈
CCSprite *girlSprite = CCSprite::createWithSpriteFrame(frame0);
CC_BREAK_IF(!girlSprite);
girlSprite->setPosition(ccp(x, y);
this->addChild(girlSprite, 2);
6. 將CCAnimation構造成CCAnimate,使精靈可以執行此動作
CCAnimate *animate = CCAnimate::create(animation);
CC_BREAK_IF(!animate);
7. 精靈執行相應動作
girlSprite->runAction(CCRepeatForever::create(animate));