mirror of https://github.com/axmolengine/axmol.git
Merge pull request #5517 from bmanGH/feature/fix_timer_cancel_bug
closed #4272: Timer::cancel always call Director::getInstance()->getScheduler() even in another Scheduler
This commit is contained in:
commit
bb86a9e350
|
@ -72,7 +72,8 @@ typedef struct _hashSelectorEntry
|
||||||
// implementation Timer
|
// implementation Timer
|
||||||
|
|
||||||
Timer::Timer()
|
Timer::Timer()
|
||||||
: _elapsed(-1)
|
: _scheduler(nullptr)
|
||||||
|
, _elapsed(-1)
|
||||||
, _runForever(false)
|
, _runForever(false)
|
||||||
, _useDelay(false)
|
, _useDelay(false)
|
||||||
, _timesExecuted(0)
|
, _timesExecuted(0)
|
||||||
|
@ -154,8 +155,9 @@ TimerTargetSelector::TimerTargetSelector()
|
||||||
{
|
{
|
||||||
}
|
}
|
||||||
|
|
||||||
bool TimerTargetSelector::initWithSelector(SEL_SCHEDULE selector, Ref* target, float seconds, unsigned int repeat, float delay)
|
bool TimerTargetSelector::initWithSelector(Scheduler* scheduler, SEL_SCHEDULE selector, Ref* target, float seconds, unsigned int repeat, float delay)
|
||||||
{
|
{
|
||||||
|
_scheduler = scheduler;
|
||||||
_target = target;
|
_target = target;
|
||||||
_selector = selector;
|
_selector = selector;
|
||||||
setupTimerWithInterval(seconds, repeat, delay);
|
setupTimerWithInterval(seconds, repeat, delay);
|
||||||
|
@ -172,7 +174,7 @@ void TimerTargetSelector::trigger()
|
||||||
|
|
||||||
void TimerTargetSelector::cancel()
|
void TimerTargetSelector::cancel()
|
||||||
{
|
{
|
||||||
Director::getInstance()->getScheduler()->unschedule(_selector, _target);
|
_scheduler->unschedule(_selector, _target);
|
||||||
}
|
}
|
||||||
|
|
||||||
// TimerTargetCallback
|
// TimerTargetCallback
|
||||||
|
@ -183,8 +185,9 @@ TimerTargetCallback::TimerTargetCallback()
|
||||||
{
|
{
|
||||||
}
|
}
|
||||||
|
|
||||||
bool TimerTargetCallback::initWithCallback(const ccSchedulerFunc& callback, void *target, const std::string& key, float seconds, unsigned int repeat, float delay)
|
bool TimerTargetCallback::initWithCallback(Scheduler* scheduler, const ccSchedulerFunc& callback, void *target, const std::string& key, float seconds, unsigned int repeat, float delay)
|
||||||
{
|
{
|
||||||
|
_scheduler = scheduler;
|
||||||
_target = target;
|
_target = target;
|
||||||
_callback = callback;
|
_callback = callback;
|
||||||
_key = key;
|
_key = key;
|
||||||
|
@ -202,7 +205,7 @@ void TimerTargetCallback::trigger()
|
||||||
|
|
||||||
void TimerTargetCallback::cancel()
|
void TimerTargetCallback::cancel()
|
||||||
{
|
{
|
||||||
Director::getInstance()->getScheduler()->unschedule(_key, _target);
|
_scheduler->unschedule(_key, _target);
|
||||||
}
|
}
|
||||||
|
|
||||||
#if CC_ENABLE_SCRIPT_BINDING
|
#if CC_ENABLE_SCRIPT_BINDING
|
||||||
|
@ -326,7 +329,7 @@ void Scheduler::schedule(const ccSchedulerFunc& callback, void *target, float in
|
||||||
}
|
}
|
||||||
|
|
||||||
TimerTargetCallback *timer = new TimerTargetCallback();
|
TimerTargetCallback *timer = new TimerTargetCallback();
|
||||||
timer->initWithCallback(callback, target, key, interval, repeat, delay);
|
timer->initWithCallback(this, callback, target, key, interval, repeat, delay);
|
||||||
ccArrayAppendObject(element->timers, timer);
|
ccArrayAppendObject(element->timers, timer);
|
||||||
timer->release();
|
timer->release();
|
||||||
}
|
}
|
||||||
|
@ -1011,7 +1014,7 @@ void Scheduler::schedule(SEL_SCHEDULE selector, Ref *target, float interval, uns
|
||||||
}
|
}
|
||||||
|
|
||||||
TimerTargetSelector *timer = new TimerTargetSelector();
|
TimerTargetSelector *timer = new TimerTargetSelector();
|
||||||
timer->initWithSelector(selector, target, interval, repeat, delay);
|
timer->initWithSelector(this, selector, target, interval, repeat, delay);
|
||||||
ccArrayAppendObject(element->timers, timer);
|
ccArrayAppendObject(element->timers, timer);
|
||||||
timer->release();
|
timer->release();
|
||||||
}
|
}
|
||||||
|
|
|
@ -43,6 +43,8 @@ NS_CC_BEGIN
|
||||||
* @{
|
* @{
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
class Scheduler;
|
||||||
|
|
||||||
typedef std::function<void(float)> ccSchedulerFunc;
|
typedef std::function<void(float)> ccSchedulerFunc;
|
||||||
//
|
//
|
||||||
// Timer
|
// Timer
|
||||||
|
@ -69,6 +71,7 @@ public:
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
|
|
||||||
|
Scheduler* _scheduler; // weak ref
|
||||||
float _elapsed;
|
float _elapsed;
|
||||||
bool _runForever;
|
bool _runForever;
|
||||||
bool _useDelay;
|
bool _useDelay;
|
||||||
|
@ -85,7 +88,7 @@ public:
|
||||||
TimerTargetSelector();
|
TimerTargetSelector();
|
||||||
|
|
||||||
/** Initializes a timer with a target, a selector and an interval in seconds, repeat in number of times to repeat, delay in seconds. */
|
/** Initializes a timer with a target, a selector and an interval in seconds, repeat in number of times to repeat, delay in seconds. */
|
||||||
bool initWithSelector(SEL_SCHEDULE selector, Ref* target, float seconds, unsigned int repeat, float delay);
|
bool initWithSelector(Scheduler* scheduler, SEL_SCHEDULE selector, Ref* target, float seconds, unsigned int repeat, float delay);
|
||||||
|
|
||||||
inline SEL_SCHEDULE getSelector() const { return _selector; };
|
inline SEL_SCHEDULE getSelector() const { return _selector; };
|
||||||
|
|
||||||
|
@ -104,7 +107,7 @@ public:
|
||||||
TimerTargetCallback();
|
TimerTargetCallback();
|
||||||
|
|
||||||
/** Initializes a timer with a target, a lambda and an interval in seconds, repeat in number of times to repeat, delay in seconds. */
|
/** Initializes a timer with a target, a lambda and an interval in seconds, repeat in number of times to repeat, delay in seconds. */
|
||||||
bool initWithCallback(const ccSchedulerFunc& callback, void *target, const std::string& key, float seconds, unsigned int repeat, float delay);
|
bool initWithCallback(Scheduler* scheduler, const ccSchedulerFunc& callback, void *target, const std::string& key, float seconds, unsigned int repeat, float delay);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @js NA
|
* @js NA
|
||||||
|
|
Loading…
Reference in New Issue