Game Development Reference
In-Depth Information
you have to let go of the basic premise that a game object is a three-colored sprite. But then, what
is a game object? In a sense, a game object can be anything you want. So you could define the
following class to represent a game object:
function GameObject() {
}
Okay, this may be going a bit too far. For now, let's assume that any game object has a position
and a velocity, but how the game object appears (if it appears) is something you don't yet deal with.
Furthermore, you want to be able to set a visibility flag so you can choose not to draw certain game
objects. So, let's create a generic GameObject class with these three member variables:
function GameObject() {
this.position = Vector2.zero;
this.velocity = Vector2.zero;
this._visible = true;
}
If you want to have a game object that is represented by a sprite, you can inherit from this base
class and add the necessary member variables.
You also add the main game-loop methods: handleInput , update , and draw . Because you don't know
yet how the game object should handle input and how it should be drawn on the screen, you leave
these two methods empty. In the update method, just as you did in the ThreeColorGameObject class,
you update the current position of the game object according to its velocity and the elapsed time:
GameObject.prototype.update = function (delta) {
this.position.addTo(this.velocity.multiply(delta));
};
Relations between Game Objects
If you want to establish a certain hierarchy between game objects, you need to identify which game
object is a part of which other game object. In terms of hierarchies, this means you need to establish
that a game object can have a parent game object . For the game object itself, it's very useful to
know who the parent is. Therefore, the GameObject class needs a member variable that refers to the
parent of the game object:
this.parent = null;
For example, imagine an object called playingField that contains all the jewels that are part of the
playing field. The playingField object can then be considered the parent of these jewels. But not
all game objects have a parent. For example, the root object doesn't have a parent. How can you
indicate that a game object doesn't have a parent? You need to set the value of the parent member
variable to “nothing”—in JavaScript programming terms, to null .
Now that you've added a parent to the game-object class, you have to deal with a few administrative
hassles in order to make sure the parent-child relationship between game objects is properly
maintained; but you get back to that later. Because of the hierarchy of game objects, you need to
make decisions about a few thing.
 
Search WWH ::




Custom Search