Check If animation is running in cocos2d-x
Asked Answered
G

2

6

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.

Gradely answered 25/9, 2012 at 11:57 Comment(0)
E
14

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." );
} 
Etamine answered 25/9, 2012 at 23:14 Comment(2)
Would you know how it is in regular cocos2d?Producer
@ThePoet I think the function is the same?Etamine
O
1

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

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

The method is inherited from CCAction. Good luck.

Octangle answered 26/9, 2012 at 8:52 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.