Game Development Reference
In-Depth Information
Assigning Identifiers to Game Objects
When you want to find game objects, it's helpful to assign identifiers to them. Not all objects need
identifiers; generally only the ones that interact with other objects do. You use the same kind of
identifiers that you used for layers earlier. For example, here you can see a few identifiers that will be
useful for the Jewel Jam game:
ID.title = 1;
ID.help_frame = 2;
ID.jewel_cart = 3;
ID.grid = 4;
ID.game_over = 5;
ID.score = 6;
ID.double_timer = 7;
ID.triple_timer = 8;
All you need to do is extend the game object classes so that you can assign an identifier to game
objects. Let's start with the GameObject class. In this class, you add a member variable to store an
identifier, and you add a parameter to the GameObject constructor method so that you can associate
an ID with an object when it's created:
function GameObject(layer, id) {
this.layer = typeof layer !== 'undefined' ? layer : 0;
this.id = typeof id !== 'undefined' ? id : 0;
this.parent = null;
this.position = Vector2.zero;
this.velocity = Vector2.zero;
this._visible = true;
}
Because not all objects need an ID (or a layer), you assign default values to these variables in case
the parameters aren't defined.
Because all game objects inherit from the GameObject class, they also have an identifier. In
many cases, you have to update the constructor of the GameObject subclasses so they pass
along the identifier to the constructor of the base ( GameObject ) class. For example, the updated
SpriteGameObject constructor is as follows:
function SpriteGameObject(sprite, layer, id) {
GameObject.call(this, layer, id);
this.sprite = sprite;
this.origin = Vector2.zero;
}
Most of the GameObject subclasses are updated in this way. Take a look at the JewelJam4 example to
see how this is done for all the different game object types.
 
Search WWH ::




Custom Search