Fix bug: timer dt time error when interval = 0.

This commit is contained in:
Vincent Yang 2015-08-13 12:55:07 +08:00
parent cdda478aea
commit d333d24177
3 changed files with 54 additions and 51 deletions

View File

@ -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);
}

View File

@ -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:

View File

@ -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();
}