MainMenu.js
The MainMenu class is a very simple class that displays an image on the screen, and calls the ApplicationManager startLevel function when a key has been pressed.
LevelEndPost.js
The last additional class is the LevelEndPost. This class is very similar to the Powerup class, except that it calls the ApplicationManager openMainMenu function when the player touches it, effectively ending the current level. [code]
One thing that is worth mentioning here is that each class that extends the GameObject class has two functions to enable them to be cleaned up: shutdown and shutdownClassName. This is because you can not override a parent function, and then still call that parent function.
In object oriented languages you can normally make a call to super.functionName to call a parent function that has been overridden. JavaScript does not have this facility: if you override a function, you do not have the ability to call the parents function of the same name. We need to be able to do this though, because the GameObjectManager needs to be able to call the shutdown function (so every class extending the GameObject needs to have a shutdown function), and the shutdown function needs to be able to shutdown any base classes (so extending classes need to be able to shutdown their parent classes). By defining a function called shutdown in every class that extends GameObject, the GameObjectManager can safely call the shutdown function and know that all the resources will be cleaned up. But the shutdown functions only call the shutdownClassName function (so the AnimatedGameObjects shutdown function would call shutdownAnimatedGameObject). This shutdownClassName does the work of cleaning up any resources, and then call the parents shutdownParentClassName function (so the AnimatedGameObjects shutdownAnimatedGameObject function would make a call to shutdownVisualGameObject). This does require writing a bit of extra code, but it is an easy way to ensure that a common function like shutdown does actually shutdown the class it is called on, and all of that classes parents as well.
That was a bit of a mouthful, but the following diagram should make it easier to understand. Take the classes VisualGameObject, AnimatedGameObject and Powerup. See how a call to the shutdown function in each class flows through to the base classes.
VisualGameObject
shutdown -> shutdownVisualGameObject -> shutdownGameObject
AnimatedGameObject
shutdown -> shutdownAnimatedGameObject -> shutdownVisualGameObject -> shutdownGameObject
Powerup
shutdown -> shutdownPowerup -> shutdownAnimatedGameObject -> shutdownVisualGameObject -> shutdownGameObject
By adding the shutdownAll function to the GameObjectManager, and implementing a system whereby the shutdown function on all classes that extend the GameObject will recursively shutdown parent classes, we have a very easy way to clean up any game objects and swap back and forth between a main menu screen. In future articles we will use this ability to load new levels, and implement high score screens.
Check out the online demo here, download the source code here, and browse the source code documentation here.