I noticed cocos2d::Schedule
has some methods to use void*
and some to use cocos2d::Ref*
as target type when I was making SpeedCC, such as Scheduler::unscheduleAllForTarget(void *target)
. This design will cause below potential problem.
class MyClassA
{
public: virtual ~MyClassA() {}
int a;
};
class MyClassB : public MyClassA, public cocos2d::Ref
{
public:
void start()
{
auto sch = cocos2d::Director::getInstance()->getScheduler();
sch->schedule(CC_SCHEDULE_SELECTOR(MyClassB::onSchedule),this,3,false);
}
void stop()
{
auto sch = cocos2d::Director::getInstance()->getScheduler();
sch->unschedule(CC_SCHEDULE_SELECTOR(MyClassB::onSchedule),this); // line A: it CAN stop schedule as expect.
// sch->unscheduleAllForTarget(this); // line B: it CAN NOT stop schedule, and no crash immediately as well.
// sch->unscheduleAllForTarget(static_cast<cocos2d::Ref*>(this)); // line C: it CAN stop schedule as expect.
}
void onSchedule(float fDelta)
{
CCLOG("onSchedule()");
}
};
I have to always use static_cast<cocos2d::Ref*>
for target in schedule to prevent this potential issue, because I don’t want to confirm the type of target every time when using cocos2d::schedule
methods, it looks ridiculous, but maybe some reasons behind it, so my question is:
What’s reason did engine team consider to design target in void*
instead of cocos2d::Ref*
? And what’s reason for some methods to use void*
and some to use cocos2d::Ref*
?