Game Shutdown Event?

From @ashes999 (taken from Github)


I’m working on a HaxeFlixel game and I need analytics integration. I need a consistent way (across HTML5 and desktop targets) to execute some of my code when the game terminates.

I opened a corresponding event on HaxeFlixel here, but it doesn’t look like they have a way to do this; there is an event handler for mobile to capture the app start/end/suspend/resume, but I don’t think it covers HTML5, and I know it doesn’t cover Neko (and presumably desktop targets).

Is there a way to do this using lime or OpenFL?

1 Like

Thanks for posting it on my behalf.

Lime does have an application.onExit event, which can be used when an application is exiting. From OpenFL, you should be able to add a handler by doing:

stage.application.onExit.add (function (exitCode) {

I’m not sure if we dispatch this event on HTML5, it appears that we use beforeUnload and dispatch window.onClose, but do not handle unload and dispatch onExit. I’m not sure if the HTML5 “unload” event is fully reliable, but this would be a good improvement. In Flash Player, there is no event when the player is unloaded to my knowledge, though handling Event.DEACTIVATE is generally good for making automatic saves, or other things that may be needed if the application quits. On mobile, this is also the case, as iOS and Android may (without warning) close your application after it goes into the background

I don’t really understand your answer, but it sounds like this is (mostly) good enough for HTML5. I’m not really worried about Flash. On iOS and Android, HaxeFlixel already has some hook that works reliably.

Thanks, I’ll try this out.

So, I’m stuck on OpenFL 3.6.1 (because that’s what HaxeFlixel uses), and it looks like application doesn’t exist on stage. The exact compiler error I get is slightly different:

openfl._legacy.display.Stage has no field application

Is there a work-around for earlier versions of OpenFL?