Check If animation is running in cocos2d-x

我只是一个虾纸丫 提交于 2019-12-05 00:37:13

问题


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.



来源:https://stackoverflow.com/questions/12582481/check-if-animation-is-running-in-cocos2d-x

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!