{"id":79,"date":"2015-02-13T17:38:09","date_gmt":"2015-02-13T17:38:09","guid":{"rendered":"http:\/\/buildanhtml5game.com\/?page_id=79"},"modified":"2015-02-13T17:38:09","modified_gmt":"2015-02-13T17:38:09","slug":"chapter-4-exercise-1-grid-layout-and-gameplay","status":"publish","type":"page","link":"http:\/\/buildanhtml5game.com\/?page_id=79","title":{"rendered":"Chapter 4, Exercise 1- Grid Layout And Gameplay"},"content":{"rendered":"<p><em>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?<\/em><\/p>\n<p>Keeping the change to createLayout, we use the <a href=\"http:\/\/buildanhtml5game.com\/?page_id=65\">grid layout from Chapter 3<\/a>:<\/p>\n<p><a href=\"http:\/\/buildanhtml5game.com\/wp-content\/uploads\/2015\/02\/Ex_4_1.png\"><img decoding=\"async\" class=\"alignnone size-full wp-image-80\" src=\"http:\/\/buildanhtml5game.com\/wp-content\/uploads\/2015\/02\/Ex_4_1.png\" alt=\"Ex_4_1\" width=\"960\" srcset=\"http:\/\/buildanhtml5game.com\/wp-content\/uploads\/2015\/02\/Ex_4_1.png 1000w, http:\/\/buildanhtml5game.com\/wp-content\/uploads\/2015\/02\/Ex_4_1-300x221.png 300w\" sizes=\"(max-width: 1000px) 100vw, 1000px\" \/><\/a><\/p>\n<p>The first thing you&#8217;ll notice is that bubbles don&#8217;t always end up in the right column when you fire them. You can fix this by changing the addBubble method in board.js:<\/p>\n<p><code>this.addBubble = function(bubble,coords){<br \/>\n&nbsp;&nbsp;var rowNum = Math.floor(coords.y \/ BubbleShoot.ui.ROW_HEIGHT);<br \/>\n&nbsp;&nbsp;var colNum = coords.x \/ BubbleShoot.ui.BUBBLE_DIMS * 2;<br \/>\n&nbsp;&nbsp;<strong>colNum = Math.floor(colNum\/2) * 2;<\/strong><br \/>\n&nbsp;&nbsp;if(!rows[rowNum])<br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;rows[rowNum] = [];<br \/>\n&nbsp;&nbsp;rows[rowNum][colNum] = bubble;<br \/>\n&nbsp;&nbsp;bubble.setRow(rowNum);<br \/>\n&nbsp;&nbsp;bubble.setCol(colNum);<br \/>\n};<br \/>\n<\/code><\/p>\n<p>The next thing is that, because of the bubble being a regimented grid, we don&#8217;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:<\/p>\n<p><code>this.getBubblesAround = function(curRow,curCol){<br \/>\n&nbsp;&nbsp;var bubbles = [];<br \/>\n&nbsp;&nbsp;for(var rowNum = curRow - 1;rowNum &lt;= curRow+1; rowNum++){<br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;<strong>var startCol = rowNum == curRow ? curCol-2 : curCol;<\/strong><br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;<strong>var endCol = rowNum == curRow ? curCol+2 : curCol;<\/strong><br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;for(var colNum = <strong>startCol<\/strong>; colNum &lt;= <strong>endCol<\/strong>; colNum++){<br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;var bubbleAt = that.getBubbleAt(rowNum,colNum);<br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if(bubbleAt && !(colNum == curCol && rowNum == curRow))<br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;bubbles.push(bubbleAt);<br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;};<br \/>\n&nbsp;&nbsp;};<br \/>\n&nbsp;&nbsp;return bubbles;<br \/>\n};<br \/>\n<\/code><\/p>\n<p>Now if you play the game it should work okay, although sometimes bubbles seem to attach themselves by a diagonal corner. I think you&#8217;ll agree the game is a lot less fun, however.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>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&#8217;ll [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"parent":22,"menu_order":41,"comment_status":"open","ping_status":"open","template":"practice-answer-page.php","meta":[],"_links":{"self":[{"href":"http:\/\/buildanhtml5game.com\/index.php?rest_route=\/wp\/v2\/pages\/79"}],"collection":[{"href":"http:\/\/buildanhtml5game.com\/index.php?rest_route=\/wp\/v2\/pages"}],"about":[{"href":"http:\/\/buildanhtml5game.com\/index.php?rest_route=\/wp\/v2\/types\/page"}],"author":[{"embeddable":true,"href":"http:\/\/buildanhtml5game.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/buildanhtml5game.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=79"}],"version-history":[{"count":1,"href":"http:\/\/buildanhtml5game.com\/index.php?rest_route=\/wp\/v2\/pages\/79\/revisions"}],"predecessor-version":[{"id":81,"href":"http:\/\/buildanhtml5game.com\/index.php?rest_route=\/wp\/v2\/pages\/79\/revisions\/81"}],"up":[{"embeddable":true,"href":"http:\/\/buildanhtml5game.com\/index.php?rest_route=\/wp\/v2\/pages\/22"}],"wp:attachment":[{"href":"http:\/\/buildanhtml5game.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=79"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}