c++ - 使用 .png 文件的动画 cocos2dx

标签 c++ objective-c cocos2d-x cocos2d-android

我有 9 个不同场景的 34 个 .png 图像文件。每次用户选择这 9 种场景中的 1 种时,我都会使用以下内容生成动画

 std::string name;
 MixingScreen::animation = CCAnimation::create();
 // load image file from local file system to CCSpriteFrame, then add into CCAnimation
 for (int i = 0; i < 34; i++)
 {
     std::stringstream st,ii;
     st << Constants::flav;
     if (i<10)
     {
         ii<<i;
        name="screen/screen02/fx/sugar/0" + st.str()+"/a_0000"+ii.str()+".png";
     }
     else
     {
         ii<<i;
        name="screen/screen02/fx/sugar/0" + st.str()+"/a_000"+ii.str()+".png";
     }
        //sprintf(szImageFileName, "Images/grossini_dance_%02d.png", i);
     MixingScreen::animation->addSpriteFrameWithFileName(name.c_str()); 
     }
     MixingScreen::animation->setDelayPerUnit(5.0f / 34.0f);

    action = CCAnimate::create(MixingScreen::animation);
    CCCallFunc *done = CCCallFunc::create(this, callfunc_selector(MixingScreen::doneMixing));
    CCSequence *readySequence = CCSequence::create(action,done,NULL);
    particles->runAction(readySequence);  

我面临的问题是当这个动画运行时,有一个时间滞后(一切都停止几秒钟)然后动画开始。有什么解决办法吗?

最佳答案

每次调用 animation->addSpriteFrameWithFileName() 时,都会创建一个新的“CCSpriteFrame”。

相反,您应该首先将 Sprite 帧添加到 CCSpriteFrameCache 并使用

animation->addSpriteFrame(CCSpriteFrameCache::sharedSpriteFrameCache()->spriteFrameWithName(""))

Ps:这些函数名称可能有点不同,但我希望你能理解。

关于c++ - 使用 .png 文件的动画 cocos2dx,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17714062/

相关文章:

iOS : How to reload a UITableView with a lot of cells without lagging the App?

iphone - 应用程序需要添加 rootViewController

iphone - objective-C 中的私有(private)和公共(public)属性

c++ - Cocos2d-x v4.0在使用Xcode 12,Xcode 12.1,Xcode 12.2的iPhone模拟器中崩溃

c++ - 为 C89 代码构建 C++ 单元测试

c++ - 在声明中为类赋值不编译

c++ - 在 c++ 中的 unordered_map 中仅访问一对元素中的一个元素

javascript - 矩阵游戏,条件是当用户选择 2 个相同颜色的球时,它应该破坏 2 个相同颜色的图案

cocos2d-iphone - 如何在 Cocos2d 中暂停动画 Sprite 动画?

c++ - 循环通过 QNetworkAccessManager get() 例程,在完成时检索顺序