switchState to existing state - playState->cutscene->platstate



  • Hello,
    I am rather new to using Haxeflixel so perhaps this is obvious, but I am making a little game where multiple scenes are loaded in my main game play state, via tiled maps, and I switch the objects being drawn based on the current scene that is loaded. I would like to have some simple cutscenes after certain events, where afterwards the user is returned to the game. I had initially conceived doing this by extending FlxState and passing the state I wanted to return to.
    To test this, I switched to the TestState, representing the cutscene I had planned.
    In PlayState.hx I have

    override public function update(elapsed:Float):Void
    {
    ...
        if (FlxG.keys.justPressed.SPACE) {
    	FlxG.switchState(new TestState(this));
        }
    ...
    }
    

    and TestState is very simple

    class TestState extends FlxState
    {
    
    	public var playState:PlayState;
    	
    	public function new(state:PlayState) 
    	{
    		super();
    		
    		playState = state;
    	}
    	
    	override public function update(elapsed:Float):Void {
    		super.update(elapsed);
    
    		
    		if (FlxG.keys.justPressed.SPACE) {
    			FlxG.switchState(playState);
    		}
    	}	
    }
    

    However, when I try to switch states I get an error:

    TypeError: Error #1009: Cannot access a property or method of a null object reference.
    at flixel.group::FlxTypedGroup/add()[C:\HaxeToolkit\haxe\lib\flixel\4,1,0\flixel\group\FlxGroup.hx:177]
    at PlayState/switchScene()[C:\Development\Haxe\TiledSidescroller\TiledSidescroller\source\PlayState.hx:62]
    at PlayState/create()[C:\Development\Haxe\TiledSidescroller\TiledSidescroller\source\PlayState.hx:34]
    at flixel::FlxGame/switchState()[C:\HaxeToolkit\haxe\lib\flixel\4,1,0\flixel\FlxGame.hx:634]
    at flixel::FlxGame/update()[C:\HaxeToolkit\haxe\lib\flixel\4,1,0\flixel\FlxGame.hx:724]
    at flixel::FlxGame/step()[C:\HaxeToolkit\haxe\lib\flixel\4,1,0\flixel\FlxGame.hx:674]
    at flixel::FlxGame/onEnterFrame()[C:\HaxeToolkit\haxe\lib\flixel\4,1,0\flixel\FlxGame.hx:541]

    Is there a way to do this, or a better way to do this? Should I consider using FlxSubState for displaying the cutscene?



  • When you switch to another state, the current state and everything in it will be destroy hence the null error. In this case the PlayState or playState.
    And yes, the FlxSubState is designed for this. You can open a substate and the current (parent) state will NOT be destroyed but only pause in the background. Simply call openSubstate() in a parent state to open a substate and close() in the substate to close it and return to the parent state.



  • @DleanJeans Well, whether the parent state pauses in the background is a bit up to you. For starters, you may need to set _parentState.persistentUpdate = false, but that won't cover things that update independently of your state, like any tweens, timers, sounds playing, and so on. So you need some kind of function to toggle those when you open the substate and toggle them back when you close it.



  • @DleanJeans Thanks for that information. I'll play around with Substates tomorrow and see how they work.


Log in to reply