The FlxState provides a segmented section of code that can be used to separate different parts of your game.

Only one FlxState can be loaded and active at a time. Override create to set up all the objects and pre-loaded content in your state, and destroy for cleanup.

States are typically used for menus, and different ‘screens’ of the game, but can be used for much more.

// change the current state
FlxG.switchState(new MyState());

Demonstration

package;

import flixel.FlxG;
import flixel.FlxState;
import flixel.text.FlxText;
import flixel.ui.FlxButton;
import flixel.util.FlxColor;
import flixel.util.FlxDestroyUtil;

class PlayState extends FlxState
{
	override function create()
	{
		super.create();
		
		var text = new FlxText();
		text.text = "I am the PlayState.";
		text.color = FlxColor.CYAN;
		text.size = 16;
		text.setBorderStyle(FlxTextBorderStyle.SHADOW, FlxColor.BLUE, 4);
		text.screenCenter();
		add(text);
		
		var button = new FlxButton(0, 0, "Switch States", switchState);
		button.screenCenter();
		button.y = text.y + text.height + 16;
		add(button);
	}
	
	private function switchState():Void
	{
		FlxG.switchState(new OtherState());
	}
}

class OtherState extends FlxState
{
	override function create()
	{
		super.create();
		
		var text = new FlxText();
		text.text = "I am the OtherState.";
		text.color = FlxColor.PINK;
		text.size = 16;
		text.setBorderStyle(FlxTextBorderStyle.SHADOW, FlxColor.RED, 4);
		text.screenCenter();
		add(text);
		
		var button = new FlxButton(0, 0, "Switch States", switchState);
		button.screenCenter();
		button.y = text.y + text.height + 16;
		add(button);
	}
	
	private function switchState():Void
	{
		FlxG.switchState(new PlayState());
	}
}