Answers:
Sprite动画非常简单。您只需创建一个CCAnimation
节点,添加要循环的图像,然后使用创建一个动作CCAnimate::actionWithDuration(float, CCAnimation, bool)
并使Sprite运行它。
例:
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);
不适用于当前版本的cocos2d-x。必须更改什么?
在新版本的CoCos2dx(2.1.1)中,您可以使用(正在运行)
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
为createWithSpriteFrame
。我对Cocos2D知之甚少,无法判断它是否有所改进。编辑会改善这个答案吗?
如果您不想使用.plist文件,并希望在当前版本的cocos2d-x中继续使用Ef Es的答案,只需更改以下几行:
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
我认为这也可以解决Ben的问题。
对于cocos2dx-v3,您需要以下内容:
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));
无法采取其他任何方式。您也可以一次又一次地重新添加相同的帧以引入暂停,但是我敢肯定还有另一种方法可以做到这一点。