Replace scene, pop scene problem [Solved]

Scene 1: Splash scene
Scene 2: Menu scene
Scene 3: Game scene
Scene 4: Pause scene

-> Start app -> Splash scene[replaceScene] -> Menu scene[replaceScene] -> Game scene[pushScene] -> Pause scene[popScene] -> Game scene(continue game ok.)

But when

-> Start app -> Splash scene[replaceScene] -> Menu scene[replaceScene] -> Game scene[pushScene] -> Pause scene[replaceScene] -> Menu scene or Game scene (restart) the app, i mean the game freezes. Debug tells me nothing. Any ideas why?

1 Like

hi @hgokturk
yes, because when you push a scene, then the Director is auto paused.
and when you pop the scene again, the Director is resumed.

But if you push a scene and then you replace to next scene the Director remains paused only.
So, if you are replacing from pause, first -

Director::getInstance()->resume()
then replace into next scene from the paused one.

Happy Coding :smile:

I tried. Seems like a function in GameScene keeps to be executed in a loop though it’s meant to be executed only one time when GameScene started. It’s called in GameScene::init(). What do you think about it? Thank you for your help by the way :slight_smile:

Is it failing because the GameScene already exists in the stack and i am trying to create it again? If so, how to properly get it done?

I am afraid that should not be the case, because
init() function is only called once, when the object is created for the class using new command in createScene()

But, when you are replacing a scene, with any other
all instances of that object are destroyed and replaced with the newly created objects.

Are you still facing the freeze problem after doing what I have posted above ?
If so, then please share me the piece of code

  • where you are pushing a scene
  • where you are replacing to a scene from the pushed scene

The only major reason could be the Director is not resumed and so could be the prime reason for freezing.

Yes, the problem still exists.
This is the pause code in GameScene.

void GameScene::startPauseScene(){
    auto scene = PauseScene::createScene();
    Director::getInstance()->pushScene(TransitionFade::create(TRANSITION_TIME, scene));
}

And the following are in PauseScene.

void PauseScene::goToMainMenuScene(cocos2d::Ref *sender)
{
    Director::getInstance()->resume();
    auto scene = MainMenuScene::createScene();
    Director::getInstance()->replaceScene(TransitionFade::create(TRANSITION_TIME, scene));
}

void PauseScene::resumeScene(cocos2d::Ref *sender)
{
    Director::getInstance()->popScene();
}

void PauseScene::restartScene(cocos2d::Ref *sender)
{
    Director::getInstance()->resume();
    auto scene = GameScene::createScene();
    Director::getInstance()->replaceScene(TransitionFade::create(TRANSITION_TIME, scene));
}

I’m sure we’ve got pretty much talented developers who could easyly come up with a solution here :smiley:

My guess is you may have some clean up issues with your scenes, or possibly incorrect initialization. You shouldn’t need the ->resume() calls, unless you’re also calling ->pause() on the Director. Have you tried a very simple setup for test? Create a new project, put a single button (or 2) in each scene/layer, and finally only use your calls to replaceScene and pushScene and popScene. I may try this and post the HelloWorld class files as example.

Main:

  • Start Game (replace to game)
    Game:
  • Pause (push pause)
    Pause:
  • Resume (pop)
  • Exit Game (replace to main)

I tried a similar one to your suggestion. Push and pop works properly but after push and pop scenes, replacescene for a new gamescene initialization fails.

How are you initializing GameScene?

Do you have the code for that test you did? Or if your current project is still simple and without any secret code could you share your .cpp/.h files in a .zip? At least the cpp/h files for GameScene, PauseScene, and MainMenuScene classes?

for (unsigned short int i = 0; i < headers.capacity(); i++){
              ..............
}

I guess in a function in GameScene.cpp, this code keeps to be executed not as many times as the capacity, but infinitely. That’s why the scene freezes. On the other hand, before popScene and pushScene operations it completely works fine. After that it mulfunctions somehow. Anything wrong with headers.capacity()?

Oh my… I just realized that i need to clear my vectors before restarting game… Otherwise the capacity is overrun and scene freezes…

Glad you figured it out! Debugging is annoying right up to the moment you find the real issue, then Victory!