Very handy for most transitions, but some like CCTransitionPageTurn requires a second param. I added another function to handle that one:
template
void popSceneWithTransition(float t, bool flag) {
CCAssert(m_pRunningScene != NULL, "running scene should not null");
m_pobScenesStack->removeLastObject();
unsigned int c = m_pobScenesStack->count();
if (c == 0) {
end();
}
else {
m_bSendCleanupToScene = true;
m_pNextScene = (CCScene*)m_pobScenesStack->objectAtIndex(c - 1);
CCScene* trans = T::create(t, m_pNextScene, flag);
m_pNextScene = trans;
}
}
But there are more special cases like CCTransitionFade, and orientations on CCTransitionFlipX/Y/Angular. Thoughts on how to combine all together, or are individual functions going to have to suffice?
Actually, that turns out to be a tiny change. I’m working with cocos2d-x-3.0beta here.
diff --git a/cocos2d/cocos/2d/CCDirector.cpp b/cocos2d/cocos/2d/CCDirector.cpp
index 2778596..02ce2a9 100644
--- a/cocos2d/cocos/2d/CCDirector.cpp
+++ b/cocos2d/cocos/2d/CCDirector.cpp
@@ -676,6 +676,13 @@ void Director::popScene(void)
}
}
+void Director::popScene(std::function<Scene*(Scene*)> wrappingFunc) {
+ popScene();
+ if (_nextScene) {
+ _nextScene = wrappingFunc(_nextScene);
+ }
+}
+
void Director::popToRootScene(void)
{
popToSceneStackLevel(1);
diff --git a/cocos2d/cocos/2d/CCDirector.h b/cocos2d/cocos/2d/CCDirector.h
index 10f46bb..2c9a08b 100644
--- a/cocos2d/cocos/2d/CCDirector.h
+++ b/cocos2d/cocos/2d/CCDirector.h
@@ -257,6 +257,7 @@ public:
* ONLY call it if there is a running scene.
*/
void popScene();
+ void popScene(std::function<Scene*(Scene*)> wrappingFunc);
/** Pops out all scenes from the stack until the root scene in the queue.
* This scene will replace the running one.
And my calling code looks like:
auto f = [](Scene* scene) {
return TransitionMoveInL::create(0.1, scene);
};
Director::getInstance()->popScene(f);
I’m sure I broke every cocos2d-x coding convention there is. But you get the idea, I hope. It’s working for me.
I ‘improved’ on it slightly by removing a single line of your code. _nextScene is used when changing between scenes in all cases, so by removing popScene() from your code, it can be used for ALL pop*() methods.
The code is a strange middleground between beautiful and disgusting, isn’t it? In any case, it works well and it’s simpler than changing creating three new pop*() methods. I’ll leave it to the devs to do a more complete job.
No… I just wanted to ask that they way you implemented transitions for pop scene. I mean the code that you changed under Director.h/cpp . Is it similar to cocos2d-x implementation of push/runWithScene ?