在头文件中声明变量:
cocos2d::Animation* g_left_animation;
定义和设置:
g_left_animation = cocos2d::Animation::create(); g_left_animation->addSpriteFrame(g_frameCache->getSpriteFrameByName("monkey5.png")); g_left_animation->addSpriteFrame(g_frameCache->getSpriteFrameByName("monkey6.png")); g_left_animation->addSpriteFrame(g_frameCache->getSpriteFrameByName("monkey7.png")); g_left_animation->addSpriteFrame(g_frameCache->getSpriteFrameByName("monkey8.png")); g_left_animation->setDelayPerUnit(2.8f / 14.0f); // 这个动画包含14帧,将会持续2.8秒. g_left_animation->setRestoreOriginalFrame(true); // 14帧播放完之后返回到第一帧 cocos2d::Animate* animate = cocos2d::Animate::create(g_left_animation);
动画使用:
g_leftMonkey->runAction(cocos2d::RepeatForever::create(g_left_animation));