Skip to content

Commit

Permalink
FlxSubState: add openCallback (#2023)
Browse files Browse the repository at this point in the history
  • Loading branch information
Ali Hassan authored and Gama11 committed Jan 11, 2017
1 parent 83f6146 commit 18132ec
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 0 deletions.
2 changes: 2 additions & 0 deletions flixel/FlxState.hx
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,8 @@ class FlxState extends FlxGroup
subState._created = true;
subState.create();
}
if (subState.openCallback != null)
subState.openCallback();
}
}

Expand Down
7 changes: 7 additions & 0 deletions flixel/FlxSubState.hx
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,11 @@ class FlxSubState extends FlxState
*/
public var closeCallback:Void->Void;

/**
* Callback method for state open/resume event.
*/
public var openCallback:Void->Void;

/**
* Helper sprite object for non-flash targets. Draws the background.
*/
Expand Down Expand Up @@ -42,6 +47,7 @@ class FlxSubState extends FlxState
{
super();
closeCallback = null;
openCallback = null;

if (FlxG.renderTile)
_bgSprite = new FlxBGSprite();
Expand Down Expand Up @@ -71,6 +77,7 @@ class FlxSubState extends FlxState
{
super.destroy();
closeCallback = null;
openCallback = null;
_parentState = null;
_bgSprite = null;
}
Expand Down
22 changes: 22 additions & 0 deletions tests/unit/src/flixel/FlxSubStateTest.hx
Original file line number Diff line number Diff line change
Expand Up @@ -64,4 +64,26 @@ class FlxSubStateTest extends FlxTest
step();
Assert.isNull(state2.subState);
}

@Test // #2023
function testCallbacks()
{
var opened = false;
var closed = false;

subState1.openCallback = function() opened = true;
subState1.closeCallback = function() closed = true;

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

Assert.isTrue(opened);
Assert.isFalse(closed);

FlxG.state.closeSubState();
step();

Assert.isTrue(opened);
Assert.isTrue(closed);
}
}

0 comments on commit 18132ec

Please sign in to comment.