cocos2dx 2.x定時器分析(3),cocos2dx2.x
時間間隔定時器分析
1、//存放時間間隔定時器的結構// Hash Element used for "selectors with interval"typedef struct _hashSelectorEntry{ ccArray *timers; //註:一個Target(如CCNode)中可能會添加多個時間間隔不同的定時器, //因為屬於同一個Target,只有一個tHashTimerEntry結構, //但是時間間隔不同,回調不同的函數,所以 //需要存放在一個數組中,數組中的元素類型是CCTimer,後面分析 CCObject *target; // hash key (retained) unsigned int timerIndex; CCTimer *currentTimer; bool currentTimerSalvaged; bool paused; UT_hash_handle hh;} tHashTimerEntry;註:在CCScheduler中,有一個成員變數,用於存放這種結構的雜湊表 // Used for "selectors with interval" struct _hashSelectorEntry *m_pHashForTimers;2、添加時間間隔定時器/** The scheduled method will be called every 'interval' seconds. If paused is YES, then it won't be called until it is resumed. If 'interval' is 0, it will be called every frame, but if so, it's recommended to use 'scheduleUpdateForTarget:' instead. If the selector is already scheduled, then only the interval parameter will be updated without re-scheduling it again. repeat let the action be repeated repeat + 1 times, use kCCRepeatForever to let the action run continuously delay is the amount of time the action will wait before it'll start @since v0.99.3, repeat and delay added in v1.1 @js NA @lua NA */ void scheduleSelector(SEL_SCHEDULE pfnSelector, CCObject *pTarget, float fInterval, unsigned int repeat, float delay, bool bPaused);-->>void CCScheduler::scheduleSelector(SEL_SCHEDULE pfnSelector, CCObject *pTarget, float fInterval, unsigned int repeat, float delay, bool bPaused){ CCAssert(pfnSelector, "Argument selector must be non-NULL"); CCAssert(pTarget, "Argument target must be non-NULL"); //同一個Target只會有一個tHashTimerEntry結構 tHashTimerEntry *pElement = NULL; HASH_FIND_INT(m_pHashForTimers, &pTarget, pElement); //如果不存在,則新增加一個 if (! pElement) { pElement = (tHashTimerEntry *)calloc(sizeof(*pElement), 1); pElement->target = pTarget; if (pTarget) { pTarget->retain(); //對對象執行一次retain操作 }//添加到m_pHashForTimers雜湊表中 HASH_ADD_INT(m_pHashForTimers, target, pElement); // Is this the 1st element ? Then set the pause level to all the selectors of this target pElement->paused = bPaused; } else { CCAssert(pElement->paused == bPaused, ""); } //同一個Target,如果有多個不同的定時器,存放到一個數組中 if (pElement->timers == NULL) { pElement->timers = ccArrayNew(10); } else { //如果某個pfnSelector回呼函數已經存在,則只修改定時器的時間間隔 for (unsigned int i = 0; i < pElement->timers->num; ++i) { CCTimer *timer = (CCTimer*)pElement->timers->arr[i]; if (pfnSelector == timer->getSelector()) { CCLOG("CCScheduler#scheduleSelector. Selector already scheduled. Updating interval from: %.4f to %.4f", timer->getInterval(), fInterval); timer->setInterval(fInterval); return; } } ccArrayEnsureExtraCapacity(pElement->timers, 1); } //如果pfnSelector回呼函數第一次加入,則建立一個CCTimer,並初始化 CCTimer *pTimer = new CCTimer(); pTimer->initWithTarget(pTarget, pfnSelector, fInterval, repeat, delay); ccArrayAppendObject(pElement->timers, pTimer); pTimer->release(); }3、移除時間間隔定時器 /** Unschedule a selector for a given target. If you want to unschedule the "update", use unscheudleUpdateForTarget. @since v0.99.3 @lua NA */ void unscheduleSelector(SEL_SCHEDULE pfnSelector, CCObject *pTarget);-->>源碼:void CCScheduler::unscheduleSelector(SEL_SCHEDULE pfnSelector, CCObject *pTarget){ // explicity handle nil arguments when removing an object if (pTarget == 0 || pfnSelector == 0) { return; } //CCAssert(pTarget); //CCAssert(pfnSelector); tHashTimerEntry *pElement = NULL; HASH_FIND_INT(m_pHashForTimers, &pTarget, pElement); if (pElement) { for (unsigned int i = 0; i < pElement->timers->num; ++i) { CCTimer *pTimer = (CCTimer*)(pElement->timers->arr[i]); //把存放pfnSelector的CCTimer移除 if (pfnSelector == pTimer->getSelector()) { if (pTimer == pElement->currentTimer && (! pElement->currentTimerSalvaged)) { pElement->currentTimer->retain(); pElement->currentTimerSalvaged = true; } ccArrayRemoveObjectAtIndex(pElement->timers, i, true); // update timerIndex in case we are in tick:, looping over the actions if (pElement->timerIndex >= i) { pElement->timerIndex--; } //如果CCTimer數量為0,則移除tHashTimerEntry結構 if (pElement->timers->num == 0) { if (m_pCurrentTarget == pElement) { m_bCurrentTargetSalvaged = true; } else { removeHashElement(pElement); } } return; } } }}-->>void CCScheduler::removeHashElement(_hashSelectorEntry *pElement){cocos2d::CCObject *target = pElement->target; ccArrayFree(pElement->timers); HASH_DEL(m_pHashForTimers, pElement); free(pElement); // make sure the target is released after we have removed the hash element // otherwise we access invalid memory when the release call deletes the target // and the target calls removeAllSelectors() during its destructor target->release(); //對target對象執行一次release}