Display coordinates from 2D Array

3983 views javascript
2

let World = {
    x: Player.playerX,
    y: Player.playerY,
    width: 5,
    height: 5,
    newX: 0,
    map: [
         [0,0,0],
         [0,0,0],
         [0,0,0]]
         ,

    createMap: function(){
        for(let row = 0; row < this.map.length; row++){
            for(let column = 0; column < this.map.length; column++){
              //Display information to console.log
              console.log(this.map[row][column]) ???
              console.log(this.map[row][0]); ???
              console.log(row[0][0]) ???
              console.log(column[0][1]) ???

        }
    }
  }
}

I would like to write to the specific coordinates to the console but nothing is working after trying for so long.

So I can check if a player is at a specific coordinate to do something.

For example.

if (player.X == row[2] and player.Y == column[1]){
  do something
}

answered question

1 Answer

4

You have to iterate over row length to get the column values for that specific row.

createMap: function(){
        for(let row = 0; row < this.map.length; row++){
            for(let column = 0; column < this.map[row].length; column++){
              console.log(this.map[row][column]);
            }
        }
}

posted this

Have an answer?

JD

Please login first before posting an answer.