diff --git a/cocos/base/CCScheduler.cpp b/cocos/base/CCScheduler.cpp index 3688f74a3c..5b692f90cf 100644 --- a/cocos/base/CCScheduler.cpp +++ b/cocos/base/CCScheduler.cpp @@ -465,13 +465,27 @@ void Scheduler::schedulePerFrame(const ccSchedulerFunc& callback, void *target, HASH_FIND_PTR(_hashForUpdates, &target, hashElement); if (hashElement) { -#if COCOS2D_DEBUG >= 1 - CCASSERT(hashElement->entry->markedForDeletion,""); -#endif - // TODO: check if priority has changed! - - hashElement->entry->markedForDeletion = false; - return; + // check if priority has changed + if ((*hashElement->list)->priority != priority) + { + if (_updateHashLocked) + { + CCLOG("warning: you CANNOT change update priority in scheduled function"); + hashElement->entry->markedForDeletion = false; + hashElement->entry->paused = paused; + return; + } + else + { + unscheduleUpdate(target); + } + } + else + { + hashElement->entry->markedForDeletion = false; + hashElement->entry->paused = paused; + return; + } } // most of the updates are going to be 0, that's way there diff --git a/tests/cpp-tests/Classes/SchedulerTest/SchedulerTest.cpp b/tests/cpp-tests/Classes/SchedulerTest/SchedulerTest.cpp index cf62c50397..33fae8fb89 100644 --- a/tests/cpp-tests/Classes/SchedulerTest/SchedulerTest.cpp +++ b/tests/cpp-tests/Classes/SchedulerTest/SchedulerTest.cpp @@ -28,7 +28,8 @@ static std::function createFunctions[] = { CL(RescheduleSelector), CL(SchedulerDelayAndRepeat), CL(SchedulerIssue2268), - CL(ScheduleCallbackTest) + CL(ScheduleCallbackTest), + CL(ScheduleUpdatePriority) }; #define MAX_LAYER (sizeof(createFunctions) / sizeof(createFunctions[0])) @@ -1148,6 +1149,47 @@ void ScheduleCallbackTest::callback(float dt) log("In the callback of schedule(CC_CALLBACK_1(XXX::member_function), this), this, ...), dt = %f", dt); } + +// ScheduleUpdatePriority + +std::string ScheduleUpdatePriority::title() const +{ + return "ScheduleUpdatePriorityTest"; +} + +std::string ScheduleUpdatePriority::subtitle() const +{ + return "click to change update priority with random value"; +} + +bool ScheduleUpdatePriority::onTouchBegan(Touch* touch, Event* event) +{ + int priority = static_cast(CCRANDOM_0_1() * 11) - 5; // -5 ~ 5 + CCLOG("change update priority to %d", priority); + scheduleUpdateWithPriority(priority); + return true; +} + +void ScheduleUpdatePriority::onEnter() +{ + SchedulerTestLayer::onEnter(); + + scheduleUpdate(); + + auto listener = EventListenerTouchOneByOne::create(); + listener->onTouchBegan = CC_CALLBACK_2(ScheduleUpdatePriority::onTouchBegan, this); + _eventDispatcher->addEventListenerWithSceneGraphPriority(listener, this); +} + +void ScheduleUpdatePriority::onExit() +{ + unscheduleUpdate(); +} + +void ScheduleUpdatePriority::update(float dt) +{ +} + //------------------------------------------------------------------ // // SchedulerTestScene diff --git a/tests/cpp-tests/Classes/SchedulerTest/SchedulerTest.h b/tests/cpp-tests/Classes/SchedulerTest/SchedulerTest.h index 57efb807a9..5728f46a3a 100644 --- a/tests/cpp-tests/Classes/SchedulerTest/SchedulerTest.h +++ b/tests/cpp-tests/Classes/SchedulerTest/SchedulerTest.h @@ -302,6 +302,21 @@ public: private: }; +class ScheduleUpdatePriority : public SchedulerTestLayer +{ +public: + CREATE_FUNC(ScheduleUpdatePriority); + + virtual std::string title() const override; + virtual std::string subtitle() const override; + void onEnter(); + void onExit(); + + virtual void update(float dt); + + bool onTouchBegan(Touch* touch, Event* event); +}; + class SchedulerTestScene : public TestScene { public: