Check If animation is running in cocos2d-x

2020-06-18 03:22发布

问题:

I am currently learning cocos2D-x and am doing some sprite animation.
My Objective is that when a button is clicked the object moves to left with some animation. Now if you click multiple times rapidly the animation takes place immediately and it looks like the bear is hoping instead of walking.

The solution to it looks simple that I should check if animation is already running and if running the new animation should not take place.

The following is a part of my code.

CCSpriteFrameCache::sharedSpriteFrameCache()->addSpriteFramesWithFile("AnimBear.plist");
CCSpriteBatchNode* spriteBatchNode = CCSpriteBatchNode::create("AnimBear.png", 8);

this->addChild(spriteBatchNode,10);
        CCArray *tempArray = new CCArray();
char buffer[15];
for (int i = 1; i <= 8 ; i++) 
    {
sprintf(buffer,"bear%i.png", i);
tempArray->addObject(CCSpriteFrameCache::sharedSpriteFrameCache()->spriteFrameByName(buffer));      
}

CCAnimation *bearWalkingAnimation = CCAnimation::create(tempArray,0.1f);
startAnimation = CCSprite::createWithSpriteFrameName("bear1.png");
startAnimation->setPosition(ccp (350 , CCDirector::sharedDirector()->getWinSize().height/2 -100));
startAnimation->setScale(0.5f);

startAnimation->setTag(5);

//Animation for bear walking    

bearAnimate = CCAnimate::create(bearWalkingAnimation);

Here bearAnimate is a global variable and i wish to know if its currently playing the animation.

How do I do it.?
Thank you.

回答1:

Assume the Sprite that runs the action is

CCSprite* bear;

I think you can use something like

bear->numberOfRunningActions()

numberOfRunningActions( ) returns an unsigned integer, so to check if there are no actions, you would have to check if it returns 0

if ( bear -> numberOfRunningActions( ) == 0 ) {
   CCLOG( "No actions running." );
} else {
   CCLOG( "Actions running." );
} 


回答2:

The bearAnimate (CCAnimate) has a method to check that.

if (bearAnimate.isDone())
    doWhatYouWant();

The method is inherited from CCAction. Good luck.



标签: cocos2d-x