Jump to content

2d array issues


IamThatGuy
 Share

Recommended Posts

Some snippets from code: (stripped out stuff to see where issue is unrelated to issue)

 

Shows how I am declaring, assigning and calling the arrays..

 

 

I am thinking the problem is the declaring of the array. I believe all my values in the 2nd dimension of the array is ALL referencing the same value.

 

Declaring is first few lines posted below. 

var Board = function(){//varsvar board = []; //tried var board = new Array(8,8);  for(var i = 0; i < 9; i++){board[i] = [];} //load board information from datathis.loadBoard = function(data){//stripped vars player and playerID //do something with k or data... $.each( data, function( key, val ) {$.each( val, function( kk, vv ) { //if no player yet, set as player 1if(playerID == 0){player = key;playerID = 1;}else if(player != key)//set as player 2{player = key;playerID = 2;} //Log is Correct, shows each x, y pass different value console.log("setting: x: " + vv.x + " y: " + vv.y + " result: " + vv.piece);board[vv.x, vv.y] = new Piece(vv.piece, playerID);});}); } this.renderPieces = function(){//2 for loops to cycle threw 2d array stripped, if( typeof board[x, y] != "undefined") //neglected{console.log("x: " + x + " y: " + y + " result: ");board[x, y].renderPiece( ((x*80)+35), ((y*80)+35) ); //renders same piece}}} var Piece = function(piece_, color_){//varsvar color = color_;var piece = piece_; var pieces = new Image();pieces.src = "resources/pieces.png"; this.renderPiece = function(x, y){ var x_offset = 0;var y_offset = 0; //greenif(this.color = 2){y_offset = 71;} switch(piece){case "a": x_offset = 340;break;case "b": x_offset = 68;break;case "c":x_offset = 136;break;case "d":x_offset = 204;break;case "e":x_offset = 272;break;} context.drawImage(pieces, x_offset, y_offset, 71, 68, x, y, 71, 68); } this.getColor = function(){return color;} this.getPiece = function(){return piece;} 

Edit: thats a little upsetting. I forgot to close the code tag and my post lost all formatting of code.. And I had to click indent over and over for nice formatting since the formatting didnt keep when copying out of my editor..

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...