Typically, when I want to open a FlxSubState
that I have defined in my code, I will use:
openSubState(new MySubState());
I have all of my create/add logic inside MySubState.create()
Which works fine, except, if there is a lot of stuff on that SubState it can cause a huge lag spike making the game 'freeze' before the substate is displayed.
If I try set a variable to my substate and re-use it, it works once, but then crashes the game when I try to open it a second time, because the substate is being auto-destoyed on close.
By default, when a FlxSubState
closes, it is destroy()
ed as well - removing all added objects, etc.
Also, since new
and create
are only called the very first time the substate is opened, anything you add
there doesn't get re-added (and you don't want to have new
/create
called every time you open the substate, since that would not stop the lag-spike)
The solution is simple: in the FlxState
that is opening your substate, there is a flag destroySubStates
set this to false
and the substates will not be destroyed on close.
...and how do you make changes to the substate between opens? You can use the openCallback
in FlxSubState
which gets triggered when you open the substate, after create
gets called (if it does) and before the substate is displayed.
So:
If you want to have an updating FlxSubState that can be re-used to cut down on 'lag' when it is opened, here's what I did:
in my PlayState:
override public function create():Void
{
...
mySubState = new MySubState();
destroySubStates = false;
...
}
public function openMySubState():Void
{
openSubState(mySubState);
}
in MySubState:
public function new():Void
{
super(0);
openCallback = refresh;
}
override public function create():Void
{
// create and add all of my elements
}
private function refresh():Void
{
// anything that changed between last time it was open and now, update .text/etc
}
AND this cuts out the lag spike everytime you open the substate!
...just remember to call mySubState.destroy();
inside your PlayState.destroy();
to clean up properly!