HaxeFlixel / flixel

Free, cross-platform 2D game engine powered by Haxe and OpenFL
https://haxeflixel.com/
MIT License
1.97k stars 436 forks source link

Add option to pause tweens during Substates switching. #2950

Open TheGaloXx opened 11 months ago

TheGaloXx commented 11 months ago

So, usually, when I open a Substate in my state, any active tween keeps playing. But everything else stops updating (like it should). I suggest adding a variable to toggle that. It worked for me with a slightly different code, but this should work aswell:

class FlxState extends FlxGroup
{
     public var keepTweensUpdate:Bool = true;

     // [...]

     function openSubState(SubState:FlxSubState)
     {
             @:privateAccess
             for (tween in FlxTween.globalManager._tweens)
        {
            tween.active = keepTweensUpdate;
        }

             // [...]
     }

     function closeSubState()
     {
             @:privateAccess
             for (tween in FlxTween.globalManager._tweens)
         {
        tween.active = true;
         }

             // [...]
     }

Geokureli commented 11 months ago

it's easy enough to extend FlxState with specific functionality, also you can improve this a little via

class PauseTweensState extends FlxState
{
    /**
     * Whether to pause all tweens when opening a substate, and cr
     */
    public var createTweenManagerForSubstates:Bool = true;

    var tweenManager:FlxTweenManager = null;

    override function openSubState(subState:FlxSubState)
    {
        super.openSubState(subState);

        if (createTweenManagerForSubstates)
        {
            tweenManager = FlxTween.globalManager;
            tweenManager.active = false;
            // create a new manager for substate tweens
            FlxTween.globalManager = new FlxTweenManager();
        }
    }

    override function closeSubState()
    {
        super.closeSubState();

        if (createTweenManagerForSubstates && tweenManager != null)
        {
            // destroy the new temp manager;
            FlxTween.globalManager.destroy();
            tweenManager.active = true;
            FlxTween.globalManager = tweenManager;
            tweenManager = null;
        }
    }
}

Your example above will resume all tweens even those paused manually by the dev, this pauses/unpauses the manager itself, and only if the value is set. This will also create a new global manager to be used by the substate

theres a few situations where this wouldn't work, like if you have substates with substates. Every way I can see doing this has caveats and all of them would be harder to detect, predict, diagnose and debug than the current method. so to me it makes more sense for people to implement their own system, and maybe have resources for people to choose from that fit their specific needs

gonna keep this issue open, so people coming here with this issue can find it, until there's a better public resource for this

TheGaloXx commented 11 months ago

Alr, sounds good to me.