How to detect if scene transition is finished?

I want to do scene transition to ingame view and start the game when scene transition is finished. When I start the game the layers in ingame view are not being created yet if transition is not finished and I get some null pointers when I do something like this:

IngameLayer* ingameLayer = dynamic_cast<IngameLayer*>(
            Director::getInstance()->getRunningScene()->getChildByTag(INGAME_LAYER_TAG));

Hence, I need to know when the transition is finished. How can I detect, when it is finished?

1 Like

Here there should be something like onEnter(in cocos2dJS) or something similar so check it out.
Otherwise, AFAIK cocos2d handles it automatically.
I means, when director runs a new scene and this scene has constructor which executed and calls whatever needs to be done.
Also, in the code that you’re doing. I feel that you should keep that code inside the constructor or if there is something like onEnter in cocos2d-x, then do it.
Anyways, where are you putting this code?

onEnter is a bad idea. I don’t know it is according to the design or it is a bug but even if onEnter is called, which means the Node is added to the scene (hence, of course, it is already created ) Director::getInstance()->getRunningScene()->getChildByTag(INGAME_LAYER_TAG)); still returns null. The problem is that running scene is being set later, after my call.

Ya, scene is definitely getting added after your call, but can we see that exact location where it is getting called and where u are adding this code so that we could have better insight so as to find out the possible solutions!!

@catch_up my point is if you have a transition in any terms that takes time, the you might want to know when it is finished, it there is callback mechanism here, the only workaround would be to schedule and event dispatching a bit later then the scene transition duration is. Like if your transition time is 2 seconds, then dispatch TRANSITION_FINISHED event after 2.1 seconds started from the time you call replaceScene. But this is also inconvenient. For example you might call it from AppDelegate.cpp, where you cannot schedule, as it is not a subclass of Node and does not have schedule `method.

There is a Node::onExitTransitionDidStart().
I think it is what you need.

2 Likes

@zhangxm I have tried that too. It is true that at that time transition is finished, but it is not true that

Director::getInstance()->getRunningScene()->getChildByTag(

will not return null. This is the problem. It is being set later.

I have run into a problem with this too. I posted a question on Stackoverflow http://stackoverflow.com/questions/36955323/issue-with-transitioning-between-scenes-using-effects

Basically I have a problem with a button in Scene A that would take us to Scene B with a transition (maybe 0.5 seconds). It is possible to tap the button fast and thus call another transition before the first has ended. It would be better if the UI could be stopped as soon as a transition starts, or if cocos2d-x would refuse to start another transition if a transition is already going. Or some other good idea? Like a way to check on Director if a transition is going or not… I don’t want to do this in code from every scene I implement… I have too many scenes for that.

Just send the duration in the UserData object and in update() of you new scene check whether that duration has finished