0

I would like to use URLLoader to save my user's state to the server when the SWF is shutting down. I have a function named saveUserState which saves the user's state correctly when called at any point except shutting down.

Within my creationComplete function, how do I add an event listener to know when the main SWF is unloading?

I have tried:

addEventListener(Event.REMOVED_FROM_STAGE, saveUserState, true, 1000, true);

But my saveUserState function is not being called when the SWF is closed. I've searched and found other answers on the Internet which require an external interface to JS which does the invocation on the browser's onbeforeunload, but I see issues with IE and I'd like to avoid requiring this JS. I also see this answer, but it's not marked as accepted and I found it not to work.

Is this possible?

Community
  • 1
  • 1
Scott
  • 3,736
  • 2
  • 26
  • 44

1 Answers1

1

Try to save user state to server when state changed. In this case you will be protected from unexpected events like disconnect or power or internet failure.

Y Borys
  • 543
  • 1
  • 5
  • 21
  • Sorry but this doesn't work. Do you have a working example perhaps? – Scott Oct 22 '14 at 15:04
  • First of all, what is "user state" in your application? What it determines? – Y Borys Oct 23 '14 at 17:59
  • It shouldn't matter, but as an example, let's say it's for a game and it describes the user's current level and acquired gold. It can be represented as a string like so: "12|542" where the user is level 12 and has 542 gold. A user may have acquired some gold since the last state save and this needs to be saved to the server before unloading the SWF. Hope that helps. – Scott Oct 23 '14 at 19:33
  • OK. You can try to save this value to server each time user rise his level and/or receive gold, it if happening not so often. Anyway, i've seen lots of games, some of them very popular, where user losing game progress when closing page or browser. If saving each time is not an option, try to save progress periodically with time intervals. – Y Borys Oct 23 '14 at 21:06
  • If this helps, there is javascript function `window.onbeforeunload`, which can can be used to call a function inside your Flex application. – Y Borys Oct 23 '14 at 21:13
  • See this http://stackoverflow.com/questions/1647088/to-execute-flex-cleanup-function-when-browser-is-closed-by-user – Y Borys Oct 23 '14 at 21:14
  • Another idea is to save progress locally on user's device as backup data. I believe this method will perform faster. – Y Borys Oct 23 '14 at 21:28