Game Development Reference
In-Depth Information
You assign the score game object to the layer ID.layer_overlays , so it's drawn on top of the
background and the score frame. You also choose appropriate positions for the frame and the score.
Finally, you assign an ID ID.score to the score game object, so that other objects can retrieve it
when needed.
A Moving Jewel Cart
To make the game a bit more exciting, you can add a feeling of pressure for the player. In the Jewel
Jam game, this is done by drawing a jewel cart that slowly moves off the screen. Once the jewel cart
is outside the screen, the game is over. Every time the player finds a correct combination of jewels,
the jewel cart moves back a little.
The jewel cart is represented in the JewelCart class. You define a couple of things in it. First, you
define how much the jewel cart should be moved back when the player finds a correct combination.
This is stored in the push member variable. You also want to set a minimal x -position, so the jewel
cart will never be drawn over the playing field. This you do in the minxpos variable. You add a
pushCart method that can be called when the player finds a correct combination of jewels. Because
the class already inherits most of its features from SpriteGameObject , the class is rather small. Have
a look at the JewelJam4 example to see the code for the class.
You add an instance of this class to the game world as follows:
var jewelCart = new JewelCart(sprites.jewelcart, ID.layer_objects, ID.jewel_cart);
jewelCart.position = new Vector2(410, 230);
jewelCart.minxpos = 410;
this.add(jewelCart);
The jewel cart object also gets an ID, so you can find it later when it needs to be pushed. You also
set its position and its minimal x -position to appropriate values. If you look at the JewelCart.js
file, you see that you assign a positive x -velocity to the cart. Because JewelCart is a subclass of
SpriteGameObject , which in turn is a subclass of GameObject , the update method updates the cart
position according to its velocity (assuming this method is called from somewhere else). Figure 15-2
shows a screenshot of the game with the new game objects (jewels, score, and jewel cart).
 
Search WWH ::




Custom Search