Skip to content

Commit

Permalink
FlxSubState: fix close() if used in two different states (#1971)
Browse files Browse the repository at this point in the history
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 Gama11 committed Oct 3, 2016
1 parent d864846 commit 51478ef
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 51478ef

Please sign in to comment.