Adding transition effect to Director::popScene
from: Cocos2d-x Cookbook by Akihito Matsuura (Packt Publishing, 2015)
By extending Director class and adding two methods, previousScene and popScene, we can add transition effects when popping a scene after pushing a scene.
#include "cocos2d.h"
DirectorEx : public cocos2d::Director
{
public:
Scene* previousScene(void);
void popScene(Scene* trans);
};
#include "DirectorEx.h"
USING_NS_CC;
Scene* DirectorEx::previousScene()
{
size_t sceneCount= _scenesStack.size();
if (sceneCount <= 1)
{
return nullptr;
}
return _scenesStack.at(sceneCount - 2);
}
void DirectorEx::popScene(Scene* trans)
{
_scenesStack.popBack();
size_t sceneCount= _scenesStack.size();
if (sceneCount == 0)
{
end();
}
else
{
_sendCleanupToScene= true;
_nextScene= trans;
}
}
This can be used as follows:
DirectorEx* directorEx= static_cast<DirectorEx*>(Director::getInstance()); Scene* prevScene= directorEx->previousScene(); Scene* pScene= TransitionFlipX::create(duration, prevScene); directorEx->popScene(pScene);