From 006578bb7417a85e8197c53847e6190c33c6273c Mon Sep 17 00:00:00 2001 From: rh101 Date: Wed, 24 Sep 2025 19:23:36 +1000 Subject: [PATCH] Make getRunningScene return consistent result for any type of scene switch. Reduce conditional statements. --- core/base/Director.cpp | 51 +++++++++++++++++++++--------------------- 1 file changed, 26 insertions(+), 25 deletions(-) diff --git a/core/base/Director.cpp b/core/base/Director.cpp index fb64823d39b4..f042548e0021 100644 --- a/core/base/Director.cpp +++ b/core/base/Director.cpp @@ -1165,41 +1165,42 @@ void Director::setNextScene() { _eventDispatcher->dispatchEvent(_beforeSetNextScene); - bool runningIsTransition = dynamic_cast(_runningScene) != nullptr; - bool newIsTransition = dynamic_cast(_nextScene) != nullptr; + auto outgoingScene = _runningScene; + _runningScene = _nextScene; + _nextScene = nullptr; - // If it is not a transition, call onExit/cleanup - if (!newIsTransition) + bool outgoingSceneIsTransition = dynamic_cast(outgoingScene) != nullptr; + + if (outgoingScene) { - if (_runningScene) - { - _runningScene->onExitTransitionDidStart(); - _runningScene->onExit(); - } + bool incomingSceneIsTransition = dynamic_cast(_runningScene) != nullptr; - // issue #709. the root node (scene) should receive the cleanup message too - // otherwise it might be leaked. - if (_sendCleanupToScene && _runningScene) + // If it is not a transition, call onExit/cleanup + if (!incomingSceneIsTransition) { - _runningScene->cleanup(); + outgoingScene->onExitTransitionDidStart(); + outgoingScene->onExit(); + + // issue #709. the root node (scene) should receive the cleanup message too + // otherwise it might be leaked. + if (_sendCleanupToScene) + { + outgoingScene->cleanup(); + } } + + outgoingScene->release(); } if (_runningScene) { - _runningScene->release(); - } - _runningScene = _nextScene; - if (_nextScene) - { - _nextScene->retain(); - } - _nextScene = nullptr; + _runningScene->retain(); - if ((!runningIsTransition) && _runningScene) - { - _runningScene->onEnter(); - _runningScene->onEnterTransitionDidFinish(); + if (!outgoingSceneIsTransition) + { + _runningScene->onEnter(); + _runningScene->onEnterTransitionDidFinish(); + } } _eventDispatcher->dispatchEvent(_afterSetNextScene);