Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Check If animation is running in cocos2d-x

Tags:

cocos2d-x

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.

like image 661
Ganesh Somani Avatar asked Sep 25 '12 11:09

Ganesh Somani


2 Answers

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." );
} 
like image 147
m.ding Avatar answered Nov 04 '22 08:11

m.ding


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

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

The method is inherited from CCAction. Good luck.

like image 40
Dish Avatar answered Nov 04 '22 07:11

Dish