From d333d24177640f1bdd2cb5594b4e3a74c5d5a252 Mon Sep 17 00:00:00 2001 From: Vincent Yang Date: Thu, 13 Aug 2015 12:55:07 +0800 Subject: [PATCH] Fix bug: timer dt time error when interval = 0. --- cocos/base/CCScheduler.cpp | 91 +++++++++---------- cocos/base/CCScheduler.h | 8 +- .../Classes/SchedulerTest/SchedulerTest.cpp | 6 ++ 3 files changed, 54 insertions(+), 51 deletions(-) diff --git a/cocos/base/CCScheduler.cpp b/cocos/base/CCScheduler.cpp index 5ee1d3780f..9e9faa409f 100644 --- a/cocos/base/CCScheduler.cpp +++ b/cocos/base/CCScheduler.cpp @@ -98,54 +98,51 @@ void Timer::update(float dt) { _elapsed = 0; _timesExecuted = 0; + return; } - else + + // accumulate elapsed time + _elapsed += dt; + + // deal with delay + if (_useDelay) { - if (_runForever && !_useDelay) - {//standard timer usage - _elapsed += dt; - if (_elapsed >= _interval) - { - trigger(); + if (_elapsed < _delay) + { + return; + } + trigger(_delay); + _elapsed = _elapsed - _delay; + _timesExecuted += 1; + _useDelay = false; + // after delay, the rest time should compare with interval + if (!_runForever && _timesExecuted > _repeat) + { //unschedule timer + cancel(); + return; + } + } + + // if _interval == 0, should trigger once every frame + float interval = (_interval > 0) ? _interval : _elapsed; + while (_elapsed >= interval) + { + trigger(interval); + _elapsed -= interval; - _elapsed -= _interval; - } - } - else - {//advanced usage - _elapsed += dt; - if (_useDelay) - { - if( _elapsed >= _delay ) - { - trigger(); - - _elapsed = _elapsed - _delay; - _timesExecuted += 1; - _useDelay = false; - } - } - else - { - if (_elapsed >= _interval) - { - trigger(); - - _elapsed -= _interval; - _timesExecuted += 1; - - } - } - - if (!_runForever && _timesExecuted > _repeat) - { //unschedule timer - cancel(); - } + if (_runForever) + { + continue; + } + _timesExecuted += 1; + if (_timesExecuted > _repeat) + { //unschedule timer + cancel(); + break; } } } - // TimerTargetSelector TimerTargetSelector::TimerTargetSelector() @@ -163,11 +160,11 @@ bool TimerTargetSelector::initWithSelector(Scheduler* scheduler, SEL_SCHEDULE se return true; } -void TimerTargetSelector::trigger() +void TimerTargetSelector::trigger(float dt) { if (_target && _selector) { - (_target->*_selector)(_elapsed); + (_target->*_selector)(dt); } } @@ -194,11 +191,11 @@ bool TimerTargetCallback::initWithCallback(Scheduler* scheduler, const ccSchedul return true; } -void TimerTargetCallback::trigger() +void TimerTargetCallback::trigger(float dt) { if (_callback) { - _callback(_elapsed); + _callback(dt); } } @@ -220,11 +217,11 @@ bool TimerScriptHandler::initWithScriptHandler(int handler, float seconds) return true; } -void TimerScriptHandler::trigger() +void TimerScriptHandler::trigger(float dt) { if (0 != _scriptHandler) { - SchedulerScriptData data(_scriptHandler,_elapsed); + SchedulerScriptData data(_scriptHandler,dt); ScriptEvent event(kScheduleEvent,&data); ScriptEngineManager::getInstance()->getScriptEngine()->sendEvent(&event); } diff --git a/cocos/base/CCScheduler.h b/cocos/base/CCScheduler.h index 5bb91b397a..f633c68d0b 100644 --- a/cocos/base/CCScheduler.h +++ b/cocos/base/CCScheduler.h @@ -57,7 +57,7 @@ public: void setupTimerWithInterval(float seconds, unsigned int repeat, float delay); - virtual void trigger() = 0; + virtual void trigger(float dt) = 0; virtual void cancel() = 0; /** triggers the timer */ @@ -86,7 +86,7 @@ public: inline SEL_SCHEDULE getSelector() const { return _selector; }; - virtual void trigger() override; + virtual void trigger(float dt) override; virtual void cancel() override; protected: @@ -106,7 +106,7 @@ public: inline const ccSchedulerFunc& getCallback() const { return _callback; }; inline const std::string& getKey() const { return _key; }; - virtual void trigger() override; + virtual void trigger(float dt) override; virtual void cancel() override; protected: @@ -123,7 +123,7 @@ public: bool initWithScriptHandler(int handler, float seconds); inline int getScriptHandler() const { return _scriptHandler; }; - virtual void trigger() override; + virtual void trigger(float dt) override; virtual void cancel() override; private: diff --git a/tests/cpp-tests/Classes/SchedulerTest/SchedulerTest.cpp b/tests/cpp-tests/Classes/SchedulerTest/SchedulerTest.cpp index 58c9799490..6733c8eaa2 100644 --- a/tests/cpp-tests/Classes/SchedulerTest/SchedulerTest.cpp +++ b/tests/cpp-tests/Classes/SchedulerTest/SchedulerTest.cpp @@ -260,6 +260,8 @@ void SchedulerPauseResumeAllUser::pause(float dt) log("Pausing, tick1 and tick2 should be called three times"); auto director = Director::getInstance(); _pausedTargets = director->getScheduler()->pauseAllTargetsWithMinPriority(Scheduler::PRIORITY_NON_SYSTEM_MIN); + unschedule(CC_SCHEDULE_SELECTOR(SchedulerPauseResumeAllUser::tick1)); + unschedule(CC_SCHEDULE_SELECTOR(SchedulerPauseResumeAllUser::tick2)); getScheduler()->resumeTarget(this); scheduleOnce(CC_SCHEDULE_SELECTOR(SchedulerPauseResumeAllUser::resume), 2.0f); } @@ -267,6 +269,10 @@ void SchedulerPauseResumeAllUser::pause(float dt) void SchedulerPauseResumeAllUser::resume(float dt) { log("Resuming"); + + schedule(CC_SCHEDULE_SELECTOR(SchedulerPauseResumeAllUser::tick1), 1.0f); + schedule(CC_SCHEDULE_SELECTOR(SchedulerPauseResumeAllUser::tick2), 1.0f); + getScheduler()->resumeTargets(_pausedTargets); _pausedTargets.clear(); }