Game Development Reference
In-Depth Information
Therefore, you can easily retrieve the game world by walking through the list of parents to get to the
root. You add a property called root to the GameObject class that does this, and it relies on recursion:
Object.defineProperty(GameObject.prototype, "root",
{
get: function () {
if (this.parent === null)
return this;
else
return this.parent.root;
}
});
The code in the property is very simple. If your current parent isn't null (meaning you have a parent),
you ask that parent for the root game object. If the parent is null , it means the game object you're
currently manipulating is at the root of the hierarchy, meaning it's the game world. In that case, you
return the current object.
Now that you've created an easy way for game objects to find each other, the following sections
introduce a couple of game-object types that are needed for the Jewel Jam game.
The Jewel Class
In order to prepare the Jewel class a bit more for the game in which it will be used, you need to
change a couple of things in this class. The biggest change is that you want to introduce more
variety in the sorts of jewels this object can represent. Basically, there are three variations: the jewel's
shape can vary, the color of the jewel can vary, and the number of jewels can vary (one, two or three
jewels). So, a jewel can have three kinds of properties (shape, color, and number). Also, for each
property, there are three variations: three different shapes, three different colors, and three different
numbers. In total, that means there are 3 × 3 × 3 = 27 possible jewel configurations (see also
Figure 15-1 ).
Figure 15-1. An overview of the different jewel types used in the Jewel Jam game
Instead of creating 27 different image files, you can store all the different varieties in a single image
file (again, see Figure 15-1 for the image). Storing multiple images in a single image file can be
beneficial for several reason. First, it provides a way for the artist to group related images together in
one file; and, second, it's much more efficient in terms of memory usage and loading speed to load a
single file and draw parts of that file instead of loading all the images separately.
 
Search WWH ::




Custom Search