Jawaban:
Animasi sprite cukup sederhana. Anda cukup membuat CCAnimation
simpul, menambahkan gambar ke lingkaran, lalu membuat tindakan menggunakan CCAnimate::actionWithDuration(float, CCAnimation, bool)
dan membuat sprite menjalankannya.
Contoh:
CCAnimation * anim = CCAnimation::animation();
// There are other several ways of storing + adding frames,
// this is the most basic using one image per frame.
anim->addFrameWithFileName("bear1.png");
anim->addFrameWithFileName("bear2.png");
anim->addFrameWithFileName("bear3.png");
anim->addFrameWithFileName("bear4.png");
anim->addFrameWithFileName("bear5.png");
anim->addFrameWithFileName("bear6.png");
anim->addFrameWithFileName("bear7.png");
anim->addFrameWithFileName("bear8.png");
CCAnimate *theAnim = CCAnimate::actionWithDuration(1.8f,anim,true);
// Duration, animation action and bool to return to frame 1 after finishing.
CCSprite *bear = CCSprite::spriteWithFile("bear1.png");
addChild(bear,0); //Don't forget to add any sprite you use as a child to the CCLayer!
bear->runAction(theAnim);
CCAnimate *theAnim = CCAnimate::actionWithDuration(1.8f,anim,true);
tidak bekerja dengan versi cocos2d-x saat ini. Apa yang harus diubah?
Dalam versi baru CoCos2dx (2.1.1) Anda dapat menggunakan (berfungsi)
CCSpriteFrameCache* cache = CCSpriteFrameCache::sharedSpriteFrameCache();
cache->addSpriteFramesWithFile("numbers.plist","numbers.png");
CCSprite* sprite = CCSprite::createWithSpriteFrame(CCSpriteFrameCache::sharedSpriteFrameCache()->spriteFrameByName("slice2_0_0.png"));
sprite->setPosition(ccp(GameScene::windowSize.width/2,GameScene::windowSize.height/3));
CCSpriteBatchNode* spriteBatchNode = CCSpriteBatchNode::create("numbers.png");
spriteBatchNode->addChild(sprite);
addChild(spriteBatchNode);
CCArray* animFrames = CCArray::createWithCapacity(10);
char str[100] = {0};
for(int i = 0; i < 10; ++i)
{
sprintf(str, "slice2_0_%d.png", i);
CCSpriteFrame* frame = cache->spriteFrameByName( str );
animFrames->addObject(frame);
}
CCAnimation* animation = CCAnimation::createWithSpriteFrames(animFrames,1.f);
sprite->runAction(CCAnimate::create(animation) );
spriteWithSpriteFrame
menjadi createWithSpriteFrame
. Saya tidak tahu cukup Cocos2D untuk mengetahui apakah ini merupakan peningkatan. Apakah hasil edit meningkatkan jawaban ini?
Jika Anda tidak ingin menggunakan file .plist dan ingin melanjutkan dengan jawaban Ef Es dengan versi cocos2d-x saat ini, cukup ubah beberapa baris seperti di bawah ini:
CCSprite * sprite = CCSprite::create("bear1.png"); // NEW - create a sprite here
CCAnimation * anim = CCAnimation::animation();
// There are other several ways of storing + adding frames,
// this is the most basic using one image per frame.
anim->addSpriteFrameWithFileName("bear1.png");
anim->addSpriteFrameWithFileName("bear2.png");
anim->addSpriteFrameWithFileName("bear3.png");
anim->addSpriteFrameWithFileName("bear4.png");
anim->addSpriteFrameWithFileName("bear5.png");
anim->addSpriteFrameWithFileName("bear6.png");
anim->addSpriteFrameWithFileName("bear7.png");
anim->addSpriteFrameWithFileName("bear8.png");
anim->setLoops(-1); // for infinit loop animation
anim->setDelayPerUnit(0.1f); //Duration per frame
//CCAnimate *theAnim = CCAnimate::actionWithDuration(1.8f,anim,true); // this wont work in newer version..
sprite->runAction(CCAnimate::create(anim) );
sprite->setPosition(ccp(200,200)); //set position of sprite in some visible area
this->addChild(sprite, 1); // cross check the Z index = 1 with your code
Saya pikir ini bisa menjadi solusi untuk pertanyaan Ben juga.
Untuk cocos2dx-v3, Anda akan membutuhkan sesuatu seperti ini:
auto cache = SpriteFrameCache::getInstance();
Vector<SpriteFrame*> frames = Vector<SpriteFrame*>();
frames.pushBack(cache->getSpriteFrameByName("open.png"));
frames.pushBack(cache->getSpriteFrameByName("closed.png"));
frames.pushBack(cache->getSpriteFrameByName("closed.png"));
cocos2d::Animation* anim = cocos2d::Animation::createWithSpriteFrames(frames, 0.1f, 1);
cocos2d::Animate* anim_action = cocos2d::Animate::create(anim);
//sprite is already added to scene elsewhere and ready to go
this->sprite->runAction(RepeatForever::create(anim_action));
Tidak bisa jalan lain. Anda juga dapat menambahkan kembali bingkai yang sama berulang-ulang untuk memberikan jeda, tapi saya yakin ada cara lain untuk melakukannya juga.