Is there an onClose event in OpenFL?
I am using a library that needs to be cleaned up during logout (i.e. close the socket so it doesn't hang until some timeout) in an OpenFL 2.2.1 application.
However, I have not been able to find any event that is triggered when I close the window using Alt+F4
or the window close button.
How can I detect that the application is shutting down to clean up my resources?
source to share
To answer the question about openfl-next
, there is an event lime.app.Application.onExit
that it inherits from lime.app.Module
. The link Application
is stored in the instance field openfl.display.Stage.application
.
So, the variant with several versions of the function will be as follows:
static function setExitHandler(func:Void->Void):Void {
#if openfl_legacy
openfl.Lib.current.stage.onQuit = function() {
func();
openfl.Lib.close();
};
#else
openfl.Lib.current.stage.application.onExit.add(function(code) {
func();
});
#end
}
And then you just
setExitHandler(function() {
trace("Quit!");
});
source to share
Part of the answer would be disabling to legacy OpenFL (with -Dopenfl-legacy
or <haxedef name="openfl-legacy" />
).
This trick forces the compiler to use the legacy API that allows Lib.stage.onQuit
.
public static function main():Void
{
// …
Lib.stage.onQuit = onClose;
// …
}
private static function onClose()
{
// …
// Cleanup of opened resources
// …
Lib.close();
}
Be sure to call Lib.close()
, otherwise your window won't close anymore!
However, this doesn't answer my question for a new one openfl-next
.
source to share