@@ -80,17 +80,17 @@ Timer::Timer()
80
80
, _interval(0 .0f )
81
81
, _aborted(false )
82
82
{
83
-
84
83
}
85
84
86
85
void Timer::setupTimerWithInterval (float seconds, unsigned int repeat, float delay)
87
86
{
88
- _elapsed = -1 ;
89
- _interval = seconds;
90
- _delay = delay;
91
- _useDelay = (_delay > 0 .0f ) ? true : false ;
92
- _repeat = repeat;
93
- _runForever = (_repeat == CC_REPEAT_FOREVER) ? true : false ;
87
+ _elapsed = -1 ;
88
+ _interval = seconds;
89
+ _delay = delay;
90
+ _useDelay = (_delay > 0 .0f ) ? true : false ;
91
+ _repeat = repeat;
92
+ _runForever = (_repeat == CC_REPEAT_FOREVER) ? true : false ;
93
+ _timesExecuted = 0 ;
94
94
}
95
95
96
96
void Timer::update (float dt)
@@ -112,12 +112,12 @@ void Timer::update(float dt)
112
112
{
113
113
return ;
114
114
}
115
+ _timesExecuted += 1 ; // important to increment before call trigger
115
116
trigger (_delay);
116
117
_elapsed = _elapsed - _delay;
117
- _timesExecuted += 1 ;
118
118
_useDelay = false ;
119
119
// after delay, the rest time should compare with interval
120
- if (!_runForever && _timesExecuted > _repeat )
120
+ if (isExhausted () )
121
121
{ // unschedule timer
122
122
cancel ();
123
123
return ;
@@ -128,11 +128,11 @@ void Timer::update(float dt)
128
128
float interval = (_interval > 0 ) ? _interval : _elapsed;
129
129
while ((_elapsed >= interval) && !_aborted)
130
130
{
131
+ _timesExecuted += 1 ; // important to increment before call trigger
131
132
trigger (interval);
132
133
_elapsed -= interval;
133
- _timesExecuted += 1 ;
134
134
135
- if (!_runForever && _timesExecuted > _repeat )
135
+ if (isExhausted () )
136
136
{
137
137
cancel ();
138
138
break ;
@@ -145,6 +145,11 @@ void Timer::update(float dt)
145
145
}
146
146
}
147
147
148
+ bool Timer::isExhausted () const
149
+ {
150
+ return !_runForever && _timesExecuted > _repeat;
151
+ }
152
+
148
153
// TimerTargetSelector
149
154
150
155
TimerTargetSelector::TimerTargetSelector ()
@@ -312,12 +317,12 @@ void Scheduler::schedule(const ccSchedulerFunc& callback, void *target, float in
312
317
{
313
318
TimerTargetCallback *timer = dynamic_cast <TimerTargetCallback*>(element->timers ->arr [i]);
314
319
315
- if (timer && key == timer->getKey ())
320
+ if (timer && !timer-> isExhausted () && key == timer->getKey ())
316
321
{
317
- CCLOG (" CCScheduler#scheduleSelector. Selector already scheduled. Updating interval from: %.4f to % .4f" , timer-> getInterval (), interval );
318
- timer->setInterval (interval);
322
+ CCLOG (" CCScheduler#schedule. Reiniting timer with interval %.4f, repeat %u, delay % .4f" , interval, repeat, delay );
323
+ timer->setupTimerWithInterval (interval, repeat, delay );
319
324
return ;
320
- }
325
+ }
321
326
}
322
327
ccArrayEnsureExtraCapacity (element->timers , 1 );
323
328
}
@@ -512,22 +517,18 @@ bool Scheduler::isScheduled(const std::string& key, void *target)
512
517
{
513
518
return false ;
514
519
}
515
- else
520
+
521
+ for (int i = 0 ; i < element->timers ->num ; ++i)
516
522
{
517
- for (int i = 0 ; i < element->timers ->num ; ++i)
523
+ TimerTargetCallback *timer = dynamic_cast <TimerTargetCallback*>(element->timers ->arr [i]);
524
+
525
+ if (timer && !timer->isExhausted () && key == timer->getKey ())
518
526
{
519
- TimerTargetCallback *timer = dynamic_cast <TimerTargetCallback*>(element->timers ->arr [i]);
520
-
521
- if (timer && key == timer->getKey ())
522
- {
523
- return true ;
524
- }
527
+ return true ;
525
528
}
526
-
527
- return false ;
528
529
}
529
530
530
- return false ; // should never get here
531
+ return false ;
531
532
}
532
533
533
534
void Scheduler::removeUpdateFromHash (struct _listEntry *entry)
@@ -734,11 +735,11 @@ bool Scheduler::isTargetPaused(void *target)
734
735
}
735
736
736
737
// We should check update selectors if target does not have custom selectors
737
- tHashUpdateEntry *elementUpdate = nullptr ;
738
- HASH_FIND_PTR (_hashForUpdates, &target, elementUpdate);
739
- if ( elementUpdate )
738
+ tHashUpdateEntry *elementUpdate = nullptr ;
739
+ HASH_FIND_PTR (_hashForUpdates, &target, elementUpdate);
740
+ if ( elementUpdate )
740
741
{
741
- return elementUpdate->entry ->paused ;
742
+ return elementUpdate->entry ->paused ;
742
743
}
743
744
744
745
return false ; // should never get here
@@ -943,13 +944,12 @@ void Scheduler::update(float dt)
943
944
if ( !_functionsToPerform.empty () ) {
944
945
_performMutex.lock ();
945
946
// fixed #4123: Save the callback functions, they must be invoked after '_performMutex.unlock()', otherwise if new functions are added in callback, it will cause thread deadlock.
946
- auto temp = _functionsToPerform;
947
- _functionsToPerform.clear ();
947
+ auto temp = std::move (_functionsToPerform);
948
948
_performMutex.unlock ();
949
- for ( const auto &function : temp ) {
949
+
950
+ for (const auto &function : temp) {
950
951
function ();
951
952
}
952
-
953
953
}
954
954
}
955
955
@@ -985,10 +985,10 @@ void Scheduler::schedule(SEL_SCHEDULE selector, Ref *target, float interval, uns
985
985
{
986
986
TimerTargetSelector *timer = dynamic_cast <TimerTargetSelector*>(element->timers ->arr [i]);
987
987
988
- if (timer && selector == timer->getSelector ())
988
+ if (timer && !timer-> isExhausted () && selector == timer->getSelector ())
989
989
{
990
- CCLOG (" CCScheduler#scheduleSelector. Selector already scheduled. Updating interval from: %.4f to % .4f" , timer-> getInterval (), interval );
991
- timer->setInterval (interval);
990
+ CCLOG (" CCScheduler#schedule. Reiniting timer with interval %.4f, repeat %u, delay % .4f" , interval, repeat, delay );
991
+ timer->setupTimerWithInterval (interval, repeat, delay );
992
992
return ;
993
993
}
994
994
}
@@ -1023,22 +1023,18 @@ bool Scheduler::isScheduled(SEL_SCHEDULE selector, Ref *target)
1023
1023
{
1024
1024
return false ;
1025
1025
}
1026
- else
1026
+
1027
+ for (int i = 0 ; i < element->timers ->num ; ++i)
1027
1028
{
1028
- for (int i = 0 ; i < element->timers ->num ; ++i)
1029
+ TimerTargetSelector *timer = dynamic_cast <TimerTargetSelector*>(element->timers ->arr [i]);
1030
+
1031
+ if (timer && !timer->isExhausted () && selector == timer->getSelector ())
1029
1032
{
1030
- TimerTargetSelector *timer = dynamic_cast <TimerTargetSelector*>(element->timers ->arr [i]);
1031
-
1032
- if (timer && selector == timer->getSelector ())
1033
- {
1034
- return true ;
1035
- }
1033
+ return true ;
1036
1034
}
1037
-
1038
- return false ;
1039
1035
}
1040
1036
1041
- return false ; // should never get here
1037
+ return false ;
1042
1038
}
1043
1039
1044
1040
void Scheduler::unschedule (SEL_SCHEDULE selector, Ref *target)
@@ -1060,7 +1056,7 @@ void Scheduler::unschedule(SEL_SCHEDULE selector, Ref *target)
1060
1056
1061
1057
if (timer && selector == timer->getSelector ())
1062
1058
{
1063
- if (timer == element->currentTimer && (! timer->isAborted () ))
1059
+ if (timer == element->currentTimer && ! timer->isAborted ())
1064
1060
{
1065
1061
timer->retain ();
1066
1062
timer->setAborted ();
0 commit comments