2012-08-08 87 views
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); 
+0

好了,加入'anim-> setDelayPerUnit(1);'似乎解决了问题。任何人都可以解释为什么DelayPerUnit和持续时间有什么不同? – Ben 2012-08-08 21:39:01

回答

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

而且,之后我就开始动画作为一个孩子添加它。

0

试试这个希望这是帮助FUL你

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