Skip to content

Commit

Permalink
FlxSubState: fix close() if used in two different states (HaxeFlixel#…
Browse files Browse the repository at this point in the history
…1971)

If you have persistent substates which you open from multiple parent states, you want the parent state of the substate to change even if the state has already been created, otherwise _parentState will be incorrect when you open the substate from a second location.
  • Loading branch information
JoeCreates authored and Aurel300 committed Apr 17, 2018
1 parent e4ba8a7 commit 5da5f66
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 1 deletion.
3 changes: 2 additions & 1 deletion flixel/FlxState.hx
Original file line number Diff line number Diff line change
Expand Up @@ -112,10 +112,11 @@ class FlxState extends FlxGroup
FlxG.inputs.onStateSwitch();
}

subState._parentState = this;

if (!subState._created)
{
subState._created = true;
subState._parentState = this;
subState.create();
}
}
Expand Down
27 changes: 27 additions & 0 deletions tests/unit/src/flixel/FlxSubStateTest.hx
Original file line number Diff line number Diff line change
Expand Up @@ -37,4 +37,31 @@ class FlxSubStateTest extends FlxTest

Assert.areEqual(subState2, FlxG.state.subState);
}

@Test // #1971
function testOpenPersistentSubStateFromNewParent()
{
var state1 = new FlxState();
var state2 = new FlxState();
state1.destroySubStates = false;
FlxG.switchState(state1);
step();
FlxG.state.openSubState(subState1);
step();

Assert.areEqual(state1.subState, subState1);
subState1.close();
step();
Assert.isNull(state1.subState);

FlxG.switchState(state2);
step();
FlxG.state.openSubState(subState1);
step();

Assert.areEqual(state2.subState, subState1);
subState1.close();
step();
Assert.isNull(state2.subState);
}
}

0 comments on commit 5da5f66

Please sign in to comment.