Game Development Reference
In-Depth Information
<script>
var stage;
var canvas;
function init() {
window.game = window.game || {};
game.main = new game.OrbDestroyer();
}
</script>
</html>
Building the States and State Events
The states and state events will be declared in a separate JavaScript file, which will hold both objects to hold these
properties. Listing 10-11 shows these two objects, written in state.js .
Listing 10-11. The State and State Event Ids Both Declared in state.js
(function () {
window.game = window.game || {};
var GameStates = {
MAIN_MENU:0,
RUN_SCENE:1,
GAME:10,
GAME_OVER:20
}
var GameStateEvents = {
MAIN_MENU:'main-menu-event',
GAME_OVER:'game-over-event',
GAME:'game-event',
}
window.game.GameStates = GameStates;
window.game.GameStateEvents = GameStateEvents;
}());
As you can see, you can easily declare and organize crucial objects within one separate file. For smaller, related
objects such as these, combining them into a single script is often a good approach.
Because this JavaScript file contains more than one object, i've opted to not use the uppercase naming
convention for its file name, which derives from other class-based programming languages. this uppercase naming
convention should be used only when creating single complex classes. remember, JavaScript does not technically have
classes, but following these classic conventions helps keep your code organized and easy to follow.
Note
 
 
Search WWH ::




Custom Search