Game Development Reference
In-Depth Information
Then you add a for instruction that traverses all the glitter scales and positions. You still need to
calculate the real scale value based on the scale value stored in the array in the update method.
If that value is between zero and the maximum scale, you don't have to do anything (scale is
increasing). If the value is between the maximum scale and twice the maximum scale, you need to
convert that value into a decreasing scale. This is done using the following instructions:
var scale = this.scales[i];
if (this.scales[i] > this._scaleMax)
scale = this._scaleMax * 2 - this.scales[i];
Each glitter should be drawn with respect to the world position of the glitter field. Therefore, you
calculate a glitter position as follows:
var pos = this.worldPosition. addTo(this.positions[i]);
The only thing left to do is to draw the glitter at the desired position, with the desired scale, using the
drawImage method from Canvas2D :
Canvas2D.drawImage(sprites.glitter, pos, 0, scale, origin);
For the complete GlitterField class, look at the JewelJamFinal program belonging to this chapter.
Adding Glitters to Game Objects
Now that you've made the generic GlitterField class, you can add a few simple extensions to your
game objects to add glitters to them. You want to do this with tastefully and not blind the player,
adding some glitters to the jewel grid as well as the moving jewel cart.
You want the glitter to appear as a rectangle on top of the jewel grid. To do that neatly, you store
the jewel grid and the accompanying glitter field in a separate GameObjectList instance. You can
then assign a position to that instance once, and the glitter field and jewel grid will be drawn at that
position. First you create this instance, called playingField :
var playingField = new GameObjectList(ID.layer_objects);
playingField.position = new Vector2(85, 150);
this.add(playingField);
Then you create the grid, just as you did in the previous versions of the game. However, now you
add it to the playingField list instead of directly to the game world:
var rows = 10, columns = 5;
var grid = new JewelGrid(rows, columns, ID.layer_objects, ID.grid);
grid.cellWidth = 85;
grid.cellHeight = 85;
grid.reset();
playingField.add(grid);
 
Search WWH ::




Custom Search