Chapter 4, Exercise 1- Grid Layout And Gameplay

In the exercises in Chapter 3, you changed createLayout to generate alternative grid patterns. Test your layouts now with the popping and orphan-dropping code. Does the code work? How do your patterns affect the feel of the game?

Keeping the change to createLayout, we use the grid layout from Chapter 3:


The first thing you’ll notice is that bubbles don’t always end up in the right column when you fire them. You can fix this by changing the addBubble method in board.js:

this.addBubble = function(bubble,coords){
  var rowNum = Math.floor(coords.y / BubbleShoot.ui.ROW_HEIGHT);
  var colNum = coords.x / BubbleShoot.ui.BUBBLE_DIMS * 2;
  colNum = Math.floor(colNum/2) * 2;
    rows[rowNum] = [];
  rows[rowNum][colNum] = bubble;

The next thing is that, because of the bubble being a regimented grid, we don’t want the getBubblesAround method to return up to 6 surrounding bubbles, but only a maximum of four: the one above, to the left, the right, and (possibly) below. So we need to change this method too:

this.getBubblesAround = function(curRow,curCol){
  var bubbles = [];
  for(var rowNum = curRow - 1;rowNum <= curRow+1; rowNum++){
    var startCol = rowNum == curRow ? curCol-2 : curCol;
    var endCol = rowNum == curRow ? curCol+2 : curCol;
    for(var colNum = startCol; colNum <= endCol; colNum++){
      var bubbleAt = that.getBubbleAt(rowNum,colNum);
      if(bubbleAt && !(colNum == curCol && rowNum == curRow))
  return bubbles;

Now if you play the game it should work okay, although sometimes bubbles seem to attach themselves by a diagonal corner. I think you’ll agree the game is a lot less fun, however.

Leave a Reply

Your email address will not be published. Required fields are marked *