I'm doing a small Cocos2d-x project, where I've been inspired by the "singleton" pattern implemented by CCDirector::sharedDirector()
method and other shared instances in Cocos2d-x. For instance, in CCDirector.cpp
we have
static CCDisplayLinkDirector *s_SharedDirector = NULL;
// ...
CCDirector* CCDirector::sharedDirector(void)
{
if (!s_SharedDirector)
{
s_SharedDirector = new CCDisplayLinkDirector();
s_SharedDirector->init();
}
return s_SharedDirector;
}
void CCDirector::purgeDirector()
{
// cleanup scheduler
getScheduler()->unscheduleAll();
// ...
// delete CCDirector
release();
}
The purgeDirector()
method purges all the other shared instances, like shared animation cache, shared sprite frame cache and so on - all cast in the same mold. I'd like to do a couple of these myself, like a shared game lobby.
I'd rather not modify CCDirector, since it is definitely subject to change.
Are there any natural place I could put my purge code? Are there possibilities for ringing a callback when cocos2d::CCDirector::sharedDirector()->end()
is called or similar? Thanks!
After a serious "Douh!" moment, I realized that purging of custom singletons would most easily be placed in the
AppDelegate::~AppDelegate()
destructor, whereAppDelegate
is provided in the cocos2d-x Xcode template. So I did.