actionscript-3flashactionscriptflixel

ActionScript 3 - How to tell when the game is closing?


I am making my first game in ActionScript and I'd like to upload it to Kongregate.

While implementing the Kongregate API (using FlxKongregate from the Flixel Power Tools), I'm trying to find the best place to call the function to disconnect from Kongregate's servers.

I can't do this is any one of the game's states, since there's a few of them that the player can move between, so disconnecting whenever one of the states closes wouldn't make any sense.

Therefore I figured the best place to implement the Kongregate API would be in the game's central registry (nothing but static methods), since that's always active in the background of the game.

What I'd like is a way to tell when the player is closing the game, and disconnect there. If I just write a dispose function, will it get automatically called? If not, what else can I do?


Solution

  • A short research indicates that you won't be able to write a function that's called when the user closes the browser. So you have several possible ways:

    1. Connect, upload scores, disconnect - all in one single operation set, at any major event of your game, so not to overload the browser with too many connect/disconnect requests.
    2. Connect, then just upload scores, and never disconnect. Maybe though Kongregate API will timeout your session, so you'll have to implement an error catching behavior and reconnect. The solution IIRC won't interfere with the session if the user reloads the page for any reason, thus your game will be able to connect even if not disconnected.
    3. Connect at game start (game session start, aka player presses "New game", something starts happening in the game, and you connect), upload scores in between, and disconnect at any kind of game leave, maybe at save&exit, gameover, win in a campaign, etc.