Hi Shri
That's nice, it's a neat way of managing game states and a perfectly valid way of doing things.
WADE, however, has a scene system, which is a superset of a state system. That is, using scenes you can do what a state system does, but scenes are a more general concept that allows you to do more.
I'll take this opportunity to say something about scenes and how they work. It's a nice flexible system, although I understand it may take time to get used to it.
In WADE there is always a current active scene. This includes everything that is on the screen, plus some other things (objects can be in the currently active scene but set to invisible for example). The states of the objects that are in the scene, including their properties and member functions, are also part of the scene. The assets used by the objects that are in the scene (images, sounds, fonts, JSON data files) are also part of the scene, which means that they can be loaded automatically when a scene is loaded. Additionally, a scene can also reference a set of "global" scripts that aren't members of any of the objects.
An important point is that scenes are data objects so they can be saved to and loaded from JSON files. This also includes scripts and functions, that get converted to strings before being exported as part of the scene.
There are few functions to interact with scenes, the key ones being:
wade.exportScene() that saves everything that is in the current scene to a data object
wade.importScene() that processes a scene data object to create and add objects to the scene, load their assets, load and execute global scripts, etc
wade.loadScene() that reads a JSON file, converts it to an object and passes it to wade.importScene()
wade.preloadScene() that does the same thing but asynchronously, without stopping the execution of the game
There are some arguments that you can pass into these functions to control exactly what they do - for example, should the current scene be cleared before adding objects from the next scene? Do you want to use the browser cache or force reloading everything? When exporting, do you want a string, or an object as the result? And so on.
So with this system it should be pretty easy to manage states. If you want a function o be executed when a scene is loaded, add a script that executes this function to the scene. Or if you prefer, add an onAddToScene function to the background object, for example. And add an onRemoveToScene function to it, with the code that you want to execute when the background is removed.
Let's make an example. Say that you are in the middle of the game and want to open a menu.
wade.app.lastScene = wade.exportScene();wade.loadScene('menu.wsc');
menu.wsc is just a JSON file. We use .wsc as the default extension for wade scenes in the editor, but it's just JSON
menu.wsc contains all the objects for the menu, their properties and, just to make an example, a "Close" button that has an onClick function (also defined inside menu.wsc) that does this:
wade.importScene(wade.app.lastScene, 0, 0, 1, 1);
So that's all the code that you need (3 lines) to effectively implement a state system and manage states using scenes.
There are caveats and it can get a bit more complicated when objects have cyclical references and cannot be easily serialized, but you get the idea :)
There are also similar (but better performing) ways of doing this if performance is an issue. The above code is simple and will take a fraction of a second to execute (WADE has its own internal cache that speeds the process up, so it's quite fast), but if you want to be faster there are other ways, such as getting a list of scene objects in the scene before loading the menu, and when exiting the menu remove any object that isn't in the list. Again, it's probably around 5 lines of code to do it that way and should be pretty straightforward.