Game Development Reference
In-Depth Information
The number of pairs to be made
The location and direction of the hint arrow
So, you can define a complete level in a variable as follows:
var myLevel = {
locked : true,
solved : false,
hint : "Don't let the penguins fall in the water!",
nrPairs : 1,
hint_arrow_x : 3,
hint_arrow_y : 1,
hint_arrow_direction : 2,
tiles : ["#.......#",
"#...r...#",
"#.......#",
"#. .#",
"#. .#",
"#. .#",
"#.......#",
"#...r...#",
"#.......#"]
};
You need to define such a variable for every level. It makes sense to store all these levels in an array .
Because the level information needs to be available from everywhere, you store the level information
in a global variable . Generally, global variables should be avoided when possible, for several reasons:
The global namespace will get cluttered with lots of global variables, potentially
slowing down script execution.
Conflicts can occur if two different JavaScript files happen to use the same
global variables.
Your source code become less clear to read, because it's difficult to keep an
overview of which data is used where.
In this case, you use a global variable because the level data needs to be accessible everywhere.
However, you can do a few things to make sure it's clear that you're using a global variable. One
thing you do is write the variable name in capital letters to stress that it's different from other, normal
variables. You also explicitly attach the variable to the global domain (which is called window in
JavaScript). Here is the variable initialization:
window.LEVELS = [];
The only thing you need to do now is fill this variable with the level information. For each level, you
add an entry to the array using the push method:
window.LEVELS.push({
locked : false,
solved : false,
hint : "Click on a penguin and select the arrow to let
it move towards the other penguin.",
 
Search WWH ::




Custom Search