2014-09-27 28 views
0

n init方法正在啓動一個CCAnimation。沒關係。在觸摸結束的方法是停止動畫。在停止的時候,我需要獲取當前動畫的圖像。在CCAnimation中獲取圖像Cocos2d-x

Player = CCSprite::create("AngleSelector1.png"); 
    Player->setPosition(ccp(size.width/2, size.height/2)); 
    this->addChild(Player); 

    //Animation 
    CCAnimation *animate = CCAnimation::create(); 
    for (int i = 1; i <=10; i++) 
    { 
     char frameName[128] = {0}; 
     sprintf(frameName, "AngleSelector%d.png", i); 
     animate->addSpriteFrameWithFileName(frameName) ; 
    } 

    animate->setDelayPerUnit(0.35f); // This animation contains 3 frames, will continuous 2.8 seconds. 
    animate->setRestoreOriginalFrame(true); // Return to the 1st frame after the 3rd frame is played. 

    CCAnimate *animaction = CCAnimate::create(animate); 
    CCRepeatForever *rt = CCRepeatForever::create(animaction); 
    Player->runAction(rt); 

    this->setTouchEnabled(true); 


void HelloWorld::ccTouchesEnded(CCSet* touches, CCEvent* event) 
{ 
    Player->stopAllActions(); 

} 
+0

,這將是在精靈的spriteFrame或displayFrame財產,根據版本 – LearnCocos2D 2014-09-27 09:52:17

+0

Cocos2dx 2.2.2正在使用。那裏有什麼特色嗎? – Sri 2014-09-27 20:29:47

+0

檢查課程參考 – LearnCocos2D 2014-09-28 11:14:58

回答

1

我解決了使用下面的代碼。圖像索引將返回當前的顯示幀

CCTexture2D* tex = Player->getTexture(); 
int imageIndex = 0; 
for (int i=0; i< animate->getFrames()->count(); i++) { 
    CCAnimationFrame *frame = (CCAnimationFrame*)animate->getFrames()->objectAtIndex(i); 
    CCTexture2D *tex2 = frame->getSpriteFrame()->getTexture(); 
    if (tex->isEqual(tex2)) { 
     imageIndex = i; 
     break; 
    } 
}