动画无法运行

时间:2012-08-08 19:20:09

标签: cocos2d-x

在cocos2d-x中,以下动画代码显示精灵,但不运行动画。 我做错了什么?

// create a CCAnimation node
CCAnimation * anim = CCAnimation::animation();

// add the images to the loop (one image per frame)
anim->addSpriteFrameWithFileName("Hero/1.png");
anim->addSpriteFrameWithFileName("Hero/2.png");
anim->addSpriteFrameWithFileName("Hero/3.png");

// create an action
CCAnimate * animation   = CCAnimate::actionWithAnimation( anim );
animation->setDuration(0.5);
CCAction * repeat       = CCRepeatForever::actionWithAction(animation);

// create a sprite to run the action
CCSprite * test = CCSprite::create("Hero/1.png");
test->setPosition( ccp( 150, 150 ) );
test->runAction( repeat );
this->addChild( test );

3 个答案:

答案 0 :(得分:0)

您需要在动画的帧上设置延迟,CCAnimate会自动计算持续时间,因此您不需要animation->setDuration()

// create a CCAnimation node
CCAnimation * anim = CCAnimation::animation();

// add the images to the loop (one image per frame)
anim->addSpriteFrameWithFileName("Hero/1.png");
anim->addSpriteFrameWithFileName("Hero/2.png");
anim->addSpriteFrameWithFileName("Hero/3.png");
anim->setDelay(.5 / 3.0);

此外,我会在将其添加为小孩后开始动画。

答案 1 :(得分:0)

试试这个希望这对你有帮助

[anim  addFrameWithFilename:Hero/1.png];
id rep_frame;
id frameactions=[CCAnimate actionWithDuration:1.0 animation:anim restoreOriginalFrame:YES];    
rep_frame=[CCRepeatForever actionWithAction:frameactions];
[self runAction:rep_frame];

答案 2 :(得分:0)

试试这段代码:

CCAnimation * anim = CCAnimation::animation();
for (int i = 1; i <= 3 ; i++)
 // add the images to the loop (one image per frame)
anim->addSpriteFrameWithFileName((CCString::createWithFormat("Hero/%d.png",i)->getCString()));
anim->setDelayPerUnit( 0.5f / 3.0f);
CCAnimate *action = CCAnimate::create(anim);

// create a sprite to run the action
CCSprite * test = CCSprite::create("Hero/1.png");
test->setPosition( ccp( 150, 150 ) );
test->runAction(CCRepeatForever::create(action)); // run action on sprite object
this->addChild( test );