void Character::dealloc() { CC_SAFE_RELEASE_NULL(_cached_shadow_scaleLoop); CC_SAFE_RELEASE_NULL(_cached_shadow_scaleToSmallerThenNormalOneTime); CC_SAFE_RELEASE_NULL(_cached_shadow_scaleToBiggerThenNormalOneTime); CC_SAFE_RELEASE_NULL(_cached_shadow_scaleToNormalOneTime); CC_SAFE_RELEASE_NULL(_cached_shadow_scaleForMidAirThenNormalOneTime); // clear out displayed-frame-cmds if(m_managedDisplayedFrameCMDs) { for(int i=0; i<m_managedDisplayedFrameCMDs->count(); i++) { CCAnimationFrame *frame = static_cast<CCAnimationFrame*>(m_managedDisplayedFrameCMDs->objectAtIndex(i)); // clear out all data in dict frame->getUserInfo()->removeAllObjects(); // set null to user info frame->setUserInfo(NULL); } m_managedDisplayedFrameCMDs->removeAllObjects(); CC_SAFE_RELEASE_NULL(m_managedDisplayedFrameCMDs); } CC_SAFE_RELEASE_NULL(m_shadow); }
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 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); }
void CCAnimationCache::parseVersion1(CCDictionary* animations) { CCSpriteFrameCache *frameCache = CCSpriteFrameCache::sharedSpriteFrameCache(); CCDictElement* pElement = NULL; CCDICT_FOREACH(animations, pElement) { CCDictionary* animationDict = (CCDictionary*)pElement->getObject(); CCArray* frameNames = (CCArray*)animationDict->objectForKey("frames"); float delay = animationDict->valueForKey("delay")->floatValue(); CCAnimation* animation = NULL; if ( frameNames == NULL ) { CCLOG("cocos2d: CCAnimationCache: Animation '%s' found in dictionary without any frames - cannot add to animation cache.", pElement->getStrKey()); continue; } CCArray* frames = CCArray::createWithCapacity(frameNames->count()); frames->retain(); CCObject* pObj = NULL; CCARRAY_FOREACH(frameNames, pObj) { const char* frameName = ((CCString*)pObj)->getCString(); CCSpriteFrame* spriteFrame = frameCache->spriteFrameByName(frameName); if ( ! spriteFrame ) { CCLOG("cocos2d: CCAnimationCache: Animation '%s' refers to frame '%s' which is not currently in the CCSpriteFrameCache. This frame will not be added to the animation.", pElement->getStrKey(), frameName); continue; } CCAnimationFrame* animFrame = new CCAnimationFrame(); animFrame->initWithSpriteFrame(spriteFrame, 1, NULL); frames->addObject(animFrame); animFrame->release(); } if ( frames->count() == 0 ) { CCLOG("cocos2d: CCAnimationCache: None of the frames for animation '%s' were found in the CCSpriteFrameCache. Animation is not being added to the Animation Cache.", pElement->getStrKey()); continue; } else if ( frames->count() != frameNames->count() ) { CCLOG("cocos2d: CCAnimationCache: An animation in your dictionary refers to a frame which is not in the CCSpriteFrameCache. Some or all of the frames for the animation '%s' may be missing.", pElement->getStrKey()); } animation = CCAnimation::create(frames, delay, 1); CCAnimationCache::sharedAnimationCache()->addAnimation(animation, pElement->getStrKey()); frames->release(); }
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()); }
CCAnimate* Image::getFullAnimation(float delay, bool invert) { CCAssert(spriteSheet != NULL, "Image getFullAnimation called on an object without spritesheet"); CCArray* animationFrames = CCArray::createWithCapacity(spritesName->count()); for(int i = 0; i < spritesName->count(); i++) { CCSpriteFrame* spriteFrame = CCSpriteFrameCache::sharedSpriteFrameCache()->spriteFrameByName(((CCString*)spritesName->objectAtIndex(invert ? spritesName->count() - i - 1 : i))->getCString()); CCAnimationFrame* frame = new CCAnimationFrame(); frame->initWithSpriteFrame(spriteFrame, 1, NULL); frame->autorelease(); animationFrames->addObject(frame); } return CCAnimate::create(CCAnimation::create(animationFrames, delay)); }
CCObject* CCAnimationFrame::copyWithZone(CCZone* pZone) { CCZone* pNewZone = NULL; CCAnimationFrame* pCopy = NULL; if(pZone && pZone->m_pCopyObject) { //in case of being called at sub class pCopy = (CCAnimationFrame*)(pZone->m_pCopyObject); } else { pCopy = new CCAnimationFrame(); pZone = pNewZone = new CCZone(pCopy); } pCopy->initWithSpriteFrame((CCSpriteFrame*)m_pSpriteFrame->copy()->autorelease(), m_fDelayUnits, m_pUserInfo != NULL ? (CCDictionary*)m_pUserInfo->copy()->autorelease() : NULL); CC_SAFE_DELETE(pNewZone); return pCopy; }
bool CCAnimation::initWithSpriteFrames(CCArray *pFrames, float delay) { CCARRAY_VERIFY_TYPE(pFrames, CCSpriteFrame*); m_uLoops = 1; m_fDelayPerUnit = delay; CCArray* pTmpFrames = CCArray::array(); setFrames(pTmpFrames); if (pFrames != NULL) { CCObject* pObj = NULL; CCARRAY_FOREACH(pFrames, pObj) { CCSpriteFrame* frame = (CCSpriteFrame*)pObj; CCAnimationFrame *animFrame = new CCAnimationFrame(); animFrame->initWithSpriteFrame(frame, 1, NULL); m_pFrames->addObject(animFrame); animFrame->release(); m_fTotalDelayUnits++; }
void Image::runFullAnimation(float delay, bool invert) { CCAssert(spriteSheet != NULL, "Image runFullAnimation called on an object without spritesheet"); CCArray* animationFrames = CCArray::createWithCapacity(spritesName->count()); for(int i = 0; i < spritesName->count(); i++) { CCSpriteFrame* spriteFrame = CCSpriteFrameCache::sharedSpriteFrameCache()->spriteFrameByName(((CCString*)spritesName->objectAtIndex(invert ? spritesName->count() - i - 1 : i))->getCString()); CCAnimationFrame* frame = new CCAnimationFrame(); frame->initWithSpriteFrame(spriteFrame, 1, NULL); frame->autorelease(); animationFrames->addObject(frame); } CCAction* action = CCRepeatForever::create(CCAnimate::create(CCAnimation::create(animationFrames, delay))); if(runningAnimation != action) { if(runningAnimation != NULL) { delegate->stopAction(runningAnimation); } delegate->runAction(action); runningAnimation = action; } }
//------------------------------------------------------------------------- // 帧更新 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; } }
//------------------------------------------------------------------------- // 播放动画,若该动作处于停止状态,则继续播放 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 ); }