Cocos2dx 帧动画创建精灵
程序员文章站
2024-03-25 20:08:04
...
用一张张图片创建
Animation* animation = Animation::create();
for (int i = 1; i <= 17; i++) {
auto frameName = __String::createWithFormat("fire/campFire%02d.png", i);
log("frameName = %s", frameName->getCString());
animation->addSpriteFrameWithFileName(frameName->getCString());//用文件名添加精灵帧
}
animation->setDelayPerUnit(0.08f);//帧延迟时间
animation->setRestoreOriginalFrame(true);//播放完回到第一帧状态
Animate* action = Animate::create(animation);//action为可执行的动画
//创建精灵
auto sprite = Sprite::create("fire/campFire02.png");
sprite->setPosition(Vec2(visibleSize.width / 2, visibleSize.height / 2));
sprite->runAction(RepeatForever::create(action));//使用动画
this->addChild(sprite);
用精灵帧缓存创建
Animation* animation = Animation::create();
for (int i = 1; i <= 4; i++) {
auto frameName = __String::createWithFormat("h%d.png", i);
log("frameName = %s", frameName->getCString());
SpriteFrame* spriteFrame = SpriteFrameCache::getInstance()
->getSpriteFrameByName(frameName->getCString());
animation->addSpriteFrame(spriteFrame);//从精灵帧缓存中添加
}
animation->setDelayPerUnit(0.15f);
animation->setRestoreOriginalFrame(true);
Animate* action = Animate::create(animation);