void YHCCActionHelper::runIntervalForeverAnimation(float interval, CCAnimation * animation, CCSprite * pSprite)
{
	CCAnimate * animate = CCAnimate::create(animation);
	CCDelayTime * delay = CCDelayTime::create(interval);
	CCSequence * sequence = CCSequence::create(animate, delay, NULL);
	CCRepeatForever * forever = CCRepeatForever::create(sequence);
	CCAnimationFrame * animationFrame = static_cast<CCAnimationFrame *>(animate->getAnimation()->getFrames().at(0));
	pSprite->setDisplayFrame(animationFrame->getSpriteFrame());
	pSprite->runAction(forever);
}
Example #2
0
void CAImageView::setDisplayFrameWithAnimationName(const char *animationName, int frameIndex)
{
    CCAssert(animationName, "CCSprite#setDisplayFrameWithAnimationName. animationName must not be NULL");

    CCAnimation *a = CCAnimationCache::sharedAnimationCache()->animationByName(animationName);

    CCAssert(a, "CCSprite#setDisplayFrameWithAnimationName: Frame not found");

    CCAnimationFrame* frame = (CCAnimationFrame*)a->getFrames()->objectAtIndex(frameIndex);

    CCAssert(frame, "CCSprite#setDisplayFrame. Invalid frame");

    setDisplayFrame(frame->getSpriteFrame());
}
//-------------------------------------------------------------------------
// 帧更新
void FKAnimateEx::Update( float dt )
{
	if( m_pAniRes == NULL )
		return;
	if( !m_pAniRes->IsValid() )
		return;
	if( m_pAniRes->IsValid() && !m_bIsResValid )
	{
		Play( m_szCurAniName, m_fSpeed, m_nLoopTime );
		m_bIsResValid = true;
	}

	if( m_pCurAnimation == NULL )
	{
		set<string>& AniNameSet = m_pAniRes->GetAniNameList();
		if( AniNameSet.empty() )
			return;

		set<string>::iterator IteBegin = AniNameSet.begin();
		Play( *IteBegin, m_fSpeed, m_nLoopTime );
		if( m_pCurAnimation == NULL )
			return;

		m_szCurAniName = (*IteBegin);
	}

	switch( m_eState )
	{
	case eAnimState_Normal:
		{
			float fDuration = m_pCurAnimation->getDuration();
			CCArray* pFrames = m_pCurAnimation->getFrames();
			int nFrames = static_cast<int>( pFrames->count() );
			float fLastTime = m_fTime;
			m_fTime += dt * m_fSpeed;
			
			if( m_fTime >= fDuration )
			{
				// 循环播放
				if( m_nLoopTime == -1 )
				{
					m_nNextFrame = 0;
					m_fTime = 0.0f;
				}
				else
				{
					m_nNextFrame = 0;
					m_fTime = 0.0f;
					m_nLoopTime--;
					if( m_nLoopTime == 0 )
					{
						m_eState = eAnimState_Stop;
						// 动作回调
						_OnAniCallback( fLastTime, m_fTime, fDuration );
						return;
					}
				}
			}

			CCSpriteFrame* pFrameToDisplay = NULL;
			for( int i = m_nNextFrame; i < nFrames; ++i )
			{
				float fSplitTime = m_vecFrameTimes.at( i );
				if( fSplitTime <= m_fTime )
				{
					CCAnimationFrame* pFrame = (CCAnimationFrame*)pFrames->objectAtIndex(i);
					pFrameToDisplay = pFrame->getSpriteFrame();
					((CCSprite*)m_pTarget)->setDisplayFrame( pFrameToDisplay );

					CCDictionary* pDict = pFrame->getUserInfo();
					if( pDict )
					{
						// 动作播放回调
					}
					m_nNextFrame = i + 1;
					break;
				}
			}

			// 动画回调
			_OnAniCallback( fLastTime, m_fTime, fDuration );
		}
		break;
	case eAnimState_Pause:
		break;
	case eAnimState_Stop:
		break;
	}
}