Esempio n. 1
0
bool CartoonFollowAction::initWithAnimation(CCAnimation *pAnimation)
{
	float singleDuration = pAnimation->getDuration();
	pAnimation->retain();
    if ( CCActionInterval::initWithDuration(singleDuration * pAnimation->getLoops() ) ) 
    {
        m_nNextFrame = 0;
        this->m_pAnimation = pAnimation;
        m_pOrigFrame = NULL;
        m_uExecutedLoops = 0;

        m_pSplitTimes.resize(pAnimation->getFrames()->count());

        float accumUnitsOfTime = 0;
        float newUnitOfTimeValue = singleDuration / pAnimation->getTotalDelayUnits();

        CCArray* pFrames = pAnimation->getFrames();
        CCARRAY_VERIFY_TYPE(pFrames, CCAnimationFrame*);

        CCObject* pObj = NULL;
        CCARRAY_FOREACH(pFrames, pObj)
        {
            CCAnimationFrame* frame = (CCAnimationFrame*)pObj;
            float value = (accumUnitsOfTime * newUnitOfTimeValue) / singleDuration;
            accumUnitsOfTime += frame->getDelayUnits();
            m_pSplitTimes.push_back(value);
        }    
//-------------------------------------------------------------------------
// 播放动画,若该动作处于停止状态,则继续播放
void FKAnimateEx::Play( const std::string& p_szAniName, float p_fSpeed, int p_nLoops, bool p_bForceUpdate )
{
	if( m_pTarget == NULL || m_pAniRes == NULL )
		return;

	if( !m_pAniRes->IsValid() )
	{
		m_szCurAniName	= p_szAniName;
		m_fSpeed		= p_fSpeed;
		m_nLoopTime		= p_nLoops;
		return;
	}

	FKAnimateExRes::SAnimationInfo* pInfo = m_pAniRes->GetAnimation( p_szAniName.c_str() );
	if( pInfo == NULL )
		return;

	CCAnimation* pCurAnimation = m_pCurAnimation;
	m_pCurAnimation = pInfo->m_pAnimation;
	if( m_pCurAnimation == NULL )
	{
		m_pCurAnimation = pCurAnimation;
		return;
	}

	m_szCurAniName = p_szAniName;
	if( m_nLoopTime > 0 )
	{
		m_nLoopTime = p_nLoops + m_pCurAnimation->getLoops();
	}
	else
	{
		m_nLoopTime = p_nLoops;
	}

	if( m_pCurAnimation == pCurAnimation )
	{
		if( p_bForceUpdate )
		{
			m_fSpeed = p_fSpeed;
			m_fTime = 0.0f;
			m_nNextFrame = 0;
			m_bIsNewLoop = true;
			m_eState = eAnimState_Normal;
		}
		return;
	}

	m_vecAniCallbacks.clear();
	unsigned int unSize = pInfo->m_vecCallbacks.size();
	for( unsigned int i = 0; i < unSize; ++i )
	{
		m_vecAniCallbacks.push_back( pInfo->m_vecCallbacks[i] );
	}

	m_fSpeed = p_fSpeed;
	m_fTime = 0.0f;
	m_nNextFrame = 0;
	m_bIsNewLoop = true;
	m_eState = eAnimState_Normal;
	m_vecFrameTimes.clear();

	CCSprite* pSprite = dynamic_cast<CCSprite*>( m_pTarget );
	if( pSprite != NULL )
	{
		pSprite->setFlipX( pInfo->m_bIsFlipX );
		pSprite->setFlipY( pInfo->m_bIsFlipY );
	}

	// 设置时间分割
	float fSingleDuration = m_pCurAnimation->getDuration();
	CCArray* pFrames = m_pCurAnimation->getFrames();
	CCARRAY_VERIFY_TYPE( pFrames, CCAnimationFrame* );

	m_vecFrameTimes.reserve( pFrames->count() );

	float fAccumUnitsOfTime = 0;
	float fNewUnitOfTime = fSingleDuration / m_pCurAnimation->getTotalDelayUnits();

	CCObject* pObj = NULL;
	CCARRAY_FOREACH( pFrames, pObj )
	{
		CCAnimationFrame* pFrame = (CCAnimationFrame*)pObj;
		float fValue = (fAccumUnitsOfTime * fNewUnitOfTime); //  / fSingleDuration
		fAccumUnitsOfTime += pFrame->getDelayUnits();
		m_vecFrameTimes.push_back( fValue );
	}