【问题标题】:Loading animations through plist files in Cocos2dxCocos2dx中通过plist文件加载动画
【发布时间】:2012-08-25 01:35:51
【问题描述】:

这些天我正在学习使用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 文件名。比如:

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

【问题讨论】:

    标签: c++ animation sprite cocos2d-x


    【解决方案1】:

    我已经解决了从加载的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);
    
    }
    

    【讨论】:

      【解决方案2】:

      //“horse.png”通过哪个batch node crate

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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2014-12-05
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2014-10-03
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多