0

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

3 回答 3

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];
于 2012-08-10T11:47:42.780 回答
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 );
于 2014-01-06T14:19:56.200 回答
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);

另外,我会在将动画添加为孩子之后开始动画。

于 2012-08-08T21:38:14.453 回答