closed #5392: add priority check for schedulePerFrame() and add a ScheduleUpdatePriority test.

This commit is contained in:
boyu0 2014-05-29 13:51:43 +08:00
parent 3d3fb6ad7e
commit 91db87a2f6
3 changed files with 79 additions and 8 deletions

View File

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

View File

@ -28,7 +28,8 @@ static std::function<Layer*()> 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<int>(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

View File

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