2012-08-30 51 views
3

這些天我正在學習使用cocos2dx。 由於現在我已經能夠加載和播放保存爲.plist文件的精靈動畫。 我加載動畫這種方式:在Cocos2dx中通過plist文件加載動畫

CCSpriteFrameCache::sharedSpriteFrameCache()->addSpriteFramesWithFile("oras.plist"); 
CCAnimation *bearWalkingAnimation = CCAnimation::create(); 

for (int i = 0 ; i < 8 ; ++i) 
{ 

    std::stringstream ss; 
    ss << "bear" << i + 1 << ".png"; 

    std::string name = ss.str(); 
    CCSpriteFrame* sprite = CCSpriteFrameCache::sharedSpriteFrameCache()>spriteFrameByName(name.c_str()); 
    bearWalkingAnimation->addSpriteFrame(sprite); 

} 

我依靠的事實,我知道圖片的名字,但我現在試圖組織一點點我的代碼。

我想知道是否有任何知道在加載時哪個plist文件appertain sprite幀。我能做到嗎?怎麼做?

換句話說,我想寫一個通用類,只能知道文件名plist加載動畫。喜歡的東西:

void MyLoaderClass::LoadAnimation(std::string plist_file_name){ ....} 

回答

3

我已經解決了從建裝plist文件CCDictionary

CCSpriteFrameCache::sharedSpriteFrameCache()->addSpriteFramesWithFile(fileName.c_str()); 

mAnimation = CCAnimation::create(); 

CCFileUtils *fileUtils = CCFileUtils::sharedFileUtils(); 
const char *fullPath = fileUtils->fullPathFromRelativePath(fileName.c_str()); 


CCDictionary *dictionary = CCDictionary::createWithContentsOfFileThreadSafe(fullPath); 
CCDictionary *framesDict = static_cast<CCDictionary*> (dictionary->objectForKey("frames")); 

CCArray *keys = framesDict->allKeys(); 

for (int i = 0 ; i < keys->count(); ++i) 
{ 
    CCString *spriteFileName = static_cast<CCString *> (keys->objectAtIndex(i)); 
    CCSpriteFrame* sprite = CCSpriteFrameCache::sharedSpriteFrameCache()->spriteFrameByName(spriteFileName->getCString()); 
    mAnimation->addSpriteFrame(sprite); 

} 
1

//"horse.png」通過一批節點箱

CCSpriteBatchNode* spritebatch = CCSpriteBatchNode::create("horse.png"); 
CCSpriteFrameCache* cache = CCSpriteFrameCache::sharedSpriteFrameCache(); 
cache->addSpriteFramesWithFile("horse.plist"); 

// "hero" is CCSprite and "horse_1.png" is a sprite in "horse.png" batch node 

英雄= CCSprite :: createWithSpriteFrameName(「horse_1.png」);

addChild(spritebatch); 
spritebatch->addChild(hero); 

CCLog("In the anim2"); 

CCArray* animFrames = CCArray::createWithCapacity(16); 
char str[100] = {0}; 
for(int i = 1; i < 16; i++) 
{ 
    // in my batch node all the sprite name as 'horse_1.png', 'horse_2.png'..... 
    sprintf(str, "horse_%i.png", i); 
    CCSpriteFrame* frame = cache->spriteFrameByName(str); 
    animFrames->addObject(frame); 
} 
CCAnimation* animation = CCAnimation::createWithSpriteFrames(animFrames, .1f); 
animation->setRestoreOriginalFrame(true); 
hero->runAction(CCRepeatForever::create(CCAnimate::create(animation)));