检查动画是否在 cocos2d-x 中运行



我目前正在学习cocos2D-x,正在做一些精灵动画。
我的目标是,当单击按钮时,对象会向左移动,并带有一些动画。现在,如果您快速单击多次,动画会立即发生,看起来熊在希望而不是行走。

它的解决方案看起来很简单,我应该检查动画是否已经在运行,以及是否不应该运行新动画。

以下是我的代码的一部分。

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);

这里 bearAnimate 是一个全局变量,我想知道它当前是否正在播放动画。

我该怎么做。?
谢谢。

假设运行动作的精灵是

CCSprite* bear;

我认为您可以使用类似的东西

bear->numberOfRunningActions()

numberOfRunningActions( )返回一个无符号整数,因此要检查是否没有操作,您必须检查它是否返回0

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

bearAnimate (CCAnimate) 有一种方法可以检查这一点。

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

该方法继承自 CCAction。祝你好运。

最新更新