2012-08-30 3 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 파일이로드되는 동안 어떤 스프라이트 프레임을 알 수 있는지 궁금합니다. 내가 할 수 있을까? 어떻게?

다른 말로하면, 파일 이름이 plist 인 애니메이션 만로드 할 수있는 일반 클래스를 작성하고 싶습니다. 나는로드 plist 파일에서 CCDictionary 구축 해결 한

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

답변

3

: 뭔가처럼

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))); 
관련 문제