Last active
August 29, 2015 14:21
-
-
Save markknol/b87bb8e342bc7934e333 to your computer and use it in GitHub Desktop.
AutoDisposer component for Flambe
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package temple.components; | |
import flambe.Component; | |
/** | |
Dispose its owner entity after a given delay. | |
Example: | |
`myEntity.add(new AutoDisposer(5);` | |
Example with callback: | |
`myEntity.add(new AutoDisposer(5, function() trace("gone")));` | |
@author Mark Knol [mediamonks] | |
*/ | |
class AutoDisposer extends Component | |
{ | |
var _delay:Float; | |
var _time:Float = 0; | |
var _onBeforeDispose:Void->Void; | |
public function new(delay:Float = 0, onBeforeDispose:Void->Void = null) | |
{ | |
_delay = (delay < 0) ? 0 : delay; | |
_onBeforeDispose = onBeforeDispose; | |
} | |
override public function onUpdate(dt:Float) | |
{ | |
_time += dt; | |
if (_time >= _delay) | |
{ | |
if (_onBeforeDispose != null) _onBeforeDispose(); | |
if (owner != null) owner.dispose(); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment