Push scene sometimes doesn't work

Hello, I have the following code that executes at a certain point:

	    DelayTime *deathdelay = DelayTime::create(2.3f);
		player->die();
		this->pauseSchedulerAndActions();
		CallFunc *death = CallFunc::create([]()
		{
			auto scene = DefeatScene::createScene(difficultylocal);
			Director::getInstance()->pushScene(scene);
		});
		runAction(Sequence::create(deathdelay, death, nullptr));

The application is supposed to wait 2.3 seconds and push the scene in the death callfunc. The problem is that sometimes it doesn’t work and the game simply remains paused. Am I doing it wrong?

You’re calling:

this->pauseSchedulerAndActions();

then you’re trying to call:

runAction(Sequence::create(deathdelay, death, nullptr));

Which is the same as:
this->runAction(…)

So, you’ve paused all actions, and then you’re trying to run an action in a paused state on the current node.

Also, at what point do you pop the scene from the stack? You’re pushing a scene every time, but you haven’t shown any code related to popping a scene off the stack, so you will effectively be adding scenes on top of each other for as long as the app is running, which seems like a mistake based on the code snippet you have provided.

There isn’t enough code provided to give a clear understanding of what you’re trying to achieve, and regardless of that, you really should be using a debugger and perhaps some console log messages to see what is going on (via CCLOG(…) etc).

1 Like