Search code examples
iosanimationcocos2d-x

Get image in CCAnimation Cocos2d-x


n init method am starting one CCAnimation. its fine. at touch ended method am stoping the animation. at the time of stop i need to get the current image of animate.

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

}

Solution

  • i solved using following code. image index will return your current display frame

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