2012-08-08 60 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);