spacer

Webref WebRef   Sitemap · Experts · Tools · Services · Newsletters · About i.com

home / experts / javascript / column47


A DOM-Based Snakes Game, Part II (3)

Developer News
Microsoft Shows Off Silverlight 4, IE9 Plans
Metasploit Expands Vulnerability Test Framework
HyperCard Reborn?

Loading the Game's Board

Now that the DOM is ready, we need to assign the right images to the right nodes. We first define the row loading function:

function loadRow(rowNum, gif) {
  for(var i = 0; i < maxColumnCount - 1; i++) {
    divNode.childNodes[domIndex(i, rowNum)].src = gif;
  }
}

Notice how easy it is to assign an image file name to a DOM's node. You just use an HTML attribute of the relevant tag (IMG's SRC for example) as a property of the node (always in lower case):

divNode.childNodes[domIndex(i, rowNum)].src = gif;

Notice the domIndex() function above. It converts a 2-D position (row and column) to an index into a 1-D array of divNode's childNodes. Here is the function:

function domIndex(x, y) {
  return (y * maxColumnCount + x);
}

The loadColumn() function is very similar to the loadRow() function:

function loadColumn(colNum, gif) {
  for(var i = 0; i < maxRowCount; i++) {
    divNode.childNodes[domIndex(colNum, i)].src = gif;
  }
}

We build the rectangular board area by calling the loadRow() method, once per row:

function loadBoardCore() {
  for(var i = 0; i < maxRowCount; i++) {
    loadRow(i, coreGif);
  }
}

Finally, we build the game's DOM similarly to how we did it for the sliding puzzle. The board consists of maxRowCount of rows. We append a <BR> tag to the end of each row, and thus forcing the new lines:

function buildBoard() {
  for(var i = 0; i < maxRowCount; i++) {
    addOneRow();
    addBr();
  }
}

http://www.internet.com

Produced by Yehuda Shiran and Tomer Shiran

internet.commediabistro.comJusttechjobs.comGraphics.com

Search:

WebMediaBrands Corporate Info

Legal Notices, Licensing, Permissions, Privacy Policy.
Advertise | Newsletters | Shopping | E-mail Offers | Freelance Jobs

webref The latest from WebReference.com Browse >
Rolling Out Your Own HTML Application Version Control · HTML 5: Client-side Storage · Working with Ajax Server Extensions
Sitemap · Experts · Tools · Services · Email a Colleague · Contact FREE Newsletters 
 The latest from internet.com
Wi-Fi Product Watch, November 2009 · Chip Market Recovering From '08 Collapse · Low-Cost Tools to Kickstart Your New Business


Created: August 30, 1999
Revised: August 30, 1999

URL: http://www.webreference.com/js/column47/load.html