Checking if animation is running in cocos2d-x

I am currently learning cocos2D-x and doing animation sprites.
My goal is that when a button is clicked, the object moves to the left with some animation. Now, if you click several times quickly, the animation happens immediately, and it looks like the bear hopes instead of walking.

The solution for it looks simple, that I have to check if the animation is running, and if a new animation should not start.

Below is the code 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 want to know if it is currently playing animation.

How can I do it?

Thanks.

+6
source share
2 answers

Suppose a sprite that performs an action

 CCSprite* bear; 

I think you can use something like

 bear->numberOfRunningActions() 

numberOfRunningActions( ) returns an unsigned integer, so to check if there are any actions, you will need to check if it returns 0

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

BearAnimate (CCAnimate) has a way to test this.

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

The method inherits from CCAction. Good luck.

+1
source

Source: https://habr.com/ru/post/926206/


All Articles