Cocos2dx 2.x timer analysis (3), cocos2dx2. x

Source: Internet
Author: User

Cocos2dx 2.x timer analysis (3), cocos2dx2. x

Time Interval timer Analysis

1. // structure of the timer with time interval // Hash Element used for "selectors with interval" typedef struct _ hashSelectorEntry {ccArray * timers; // Note: A Target (such as CCNode) multiple timers with different time intervals may be added. // because the timer belongs to the same Target, there is only one tHashTimerEntry structure. // but the time interval is different, and different functions are called back, therefore, // It must be stored in an array. The element type in the array is CCTimer, followed by CCObject * target; // hash key (retained) unsigned int timerIndex; CCTimer * currentTimer; bool currentTimerSalvaged; bool paused; UT_hash_handle hh ;} THashTimerEntry; note: In ccsched, there is a member variable Used to store the hash table of this structure/Used for "selectors with interval" struct _ hashSelectorEntry * m_pHashForTimers; 2. Add The interval Timer/** 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 pfneuron Lector, CCObject * pTarget, float fInterval, unsigned int repeat, float delay, bool bPaused); --> void identifier: struct (effecpfnselector, 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"); // the same Target only has one tHashTimerEntry structure tHashTim ErEntry * pElement = NULL; HASH_FIND_INT (m_pHashForTimers, & pTarget, pElement); // if not, a new if (! PElement) {pElement = (tHashTimerEntry *) calloc (sizeof (* pElement), 1); pElement-> target = pTarget; if (pTarget) {pTarget-> retain (); // perform a retain operation on the object} // Add it to the HASH_ADD_INT (m_pHashForTimers, target, pElement) hash table of m_pHashForTimers; // 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 ,"");} // for the same Target, if there are multiple different timers, store them in an array if (pElement-> timers = NULL) {pElement-> timers = ccArrayNew (10);} else {// If a pfnSelector callback function already exists, modify only the timer interval 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 );} // If the pfnSelector callback function is added for the first time, a CCTimer is created and CCTimer * pTimer = new CCTimer (); pTimer-> initWithTarget (pTarget, pfnSelector, f Interval, repeat, delay); ccArrayAppendObject (pElement-> timers, pTimer); pTimer-> release ();} 3. Remove the time interval Timer/** Unschedule a selector for a given target. if you want to unschedule the "update", use unscheudleUpdateForTarget. @ since v0.99.3 @ lua NA */void Merge (SEL_SCHEDULE pfnSelector, CCObject * pTarget); --> source code: void ccschedget: SEL_SCHEDULE pfnSelector, CCObject pTa * Rget) {// 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]); // set the CCTi of the pfnSelector. Mer remove 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 --;} // if the number of CCTimer is 0, remove the tHashTimerEntry structure if (pElement-> timers-> num = 0) {if (m_pCurrentTarget = pElement) {m_bCurrentTargetSalvaged = true ;} else {removeHashElement (pElement) ;}}return ;}}--> void ccsched;:: 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 CILS removeAllSelectors () during its destructor target-> release (); // execute release once on the target object}


Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.