2012-09-25 111 views
6

运行目前我正在学习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是一个全局变量,我想知道它是否正在播放动画。

如何做呢?
谢谢。

回答

13

假设运行操作的雪碧是

CCSprite* bear; 

我想你可以使用类似

bear->numberOfRunningActions() 

numberOfRunningActions()返回一个无符号整数,所以要检查是否有任何行动,你会必须检查它是否返回0

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

@LanceGray:谢谢你,伙计! –

+0

你知道它是如何在常规的cocos2d? – RollRoll

+1

@ThePoet我觉得功能是一样的吗? –

1

bearAnimate(CCAnimate)有一个方法来检查。

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

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