如何在Cocos2d-x中播放动画?


Answers:


9

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);   

谢谢,但是HelloWorld :: getPlayer()是什么?我在添加runAction(laanim)的iPhone模拟器上崩溃了;对我的代码。
2012年

您可以使用一个Sprite或任何其他想要的节点,我有一个方法可以返回一个静态的_player,我之前已经对其进行了初始化。
MLProgrammer-CiM 2012年

我为清楚起见,现在对其进行了编辑:)欢迎您。
MLProgrammer-CiM 2012年

CCAnimate *theAnim = CCAnimate::actionWithDuration(1.8f,anim,true); 不适用于当前版本的cocos2d-x。必须更改什么?
2012年

可能他们最近修改了很多东西。Dunno现在,只需检查他们的文档,也许您就需要更多/更少一个参数。
MLProgrammer-CiM 2012年

5

在新版本的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) );

在编辑审阅队列中对此问题进行了编辑,并将其重命名spriteWithSpriteFramecreateWithSpriteFrame。我对Cocos2D知之甚少,无法判断它是否有所改进。编辑会改善这个答案吗?
Anko 2013年

2

如果您不想使用.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的问题。


0

对于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));

无法采取其他任何方式。您也可以一次又一次地重新添加相同的帧以引入暂停,但是我敢肯定还有另一种方法可以做到这一点。


您能告诉我如何运行相同的动画,但将精灵水平翻转吗?我已经为此苦苦了一段时间,而setFlippedX(true)似乎没有做到这一点……
Kaizer Sozay
By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.