diff --git a/flixel/FlxG.hx b/flixel/FlxG.hx index a01f0d160b..0703804233 100644 --- a/flixel/FlxG.hx +++ b/flixel/FlxG.hx @@ -297,26 +297,18 @@ class FlxG } /** - * Switch from the current game state to the one specified here. + * Attempts to switch from the current game state to `nextState`. + * The state switch is successful if `switchTo()` of the current `state` returns `true`. */ - public static inline function switchState(State:FlxState):Void + public static inline function switchState(nextState:FlxState):Void { - //If a transition is required - if (game._state.isTransitionNeeded()) - { - //Do the transition and exit early - game._state.transitionToState(State); - return; - } - //Otherwise do the switch normally - else - { - game._requestedState = State; - } + if (state.switchTo(nextState)) + game._requestedState = nextState; } /** * Request a reset of the current game state. + * Calls `switchState()` with a new instance of the current `state`. */ public static inline function resetState():Void { diff --git a/flixel/FlxState.hx b/flixel/FlxState.hx index fdfd3d21f2..d0505d7cb5 100644 --- a/flixel/FlxState.hx +++ b/flixel/FlxState.hx @@ -130,22 +130,14 @@ class FlxState extends FlxGroup } /** - * Checked by FlxG.switchState to see if a transition is required before switching states - * @return - */ - public function isTransitionNeeded():Bool - { - //override in your subclass to enable this functionality - return false; - } - - /** - * Perform a visual transition, and when it's complete, call FlxG.switchState - * @param State + * Called from `FlxG.switchState()`. If `false` is returned, the state + * switch is cancelled - the default implementation returns `true`. + * + * Useful for customizing state switches, e.g. for transition effects. */ - public function transitionToState(State:FlxState):Void + public function switchTo(nextState:FlxState):Bool { - //override in your subclass to enable this functionality + return true; } /**