0

EDIT: I've included more detail in the getNeighbor function

I know the following code is long, but I summarized as much as possible. I have the following code creating a hex map:

function Hex (col, row, ter) { //new class of Hexes with row, column, and terrain
  this.col = col;
  this.row = row;
  this.ter = ter; }

function Map (num_col, num_row) { //create a map of Hexes of dimension num_col x num_row
    this.num_col = num_col;
    this.num_row = num_row;
    this.hexArray = new Array();
    for (i=0; i<num_col; i++) {            //creates a 2D array of Hexes with "blank" terrain
        this.hexArray[i] = new Array();
        for (j=0; j<num_row; j++) {
            this.hexArray[i][j] = new Hex(i, j, "blank"); }}

    this.step1 = function() { 
    //assigns a terrain value to 30% of the hexes in the map ("seeds" the map) and returns an array called step1Array whose values are the seeded Hexes.

    this.getNeighbor = function(hex) { //returns all the neighbors of the given hex in an array called neighborArray 
        var delta = [[1, -1, 0], [1, 0, -1], [0, 1, -1],
                 [-1, 1, 0], [-1, 0, 1], [0, -1, 1]];
        this.neighborArray = new Array();

        for (i=0; i<delta.length; i++) {
            //get the coordinates of the neighbors and store them as newCol and newRow
            if (newCol<0 || newRow<0 || newCol>this.num_col-1 || newRow>this.num_row-1  //skip any hexes that are out of bounds
                || this.hexArray[newCol][newRow].ter !== "blank") continue;             //or that are already seeded
            this.neighborArray.push(this.hexArray[newCol][newRow]); }

    return this.neighborArray;
}

    this.step2 = function() { //assigns a terrain value to all "blank" neighbors of the seeded hexes
        for (i=0; i<this.step1Array.length; i++) {
            this.getNeighbor(this.step1Array[i]);
            if (this.neighborArray.length === 0) continue;  //if there are no "blank" neighbors, continue to the next seeded Hex
            else {
                for (j=0; j<this.neighborArray.length; j++) {    //else assign each blank neighbor a terrain value
                        var percent = Math.random();
                        var temp_col = this.neighborArray[i].col;
                        var temp_row = this.neighborArray[i].row;
                //based on the value of "percent", assign this.hexArray[temp_col][temp_row].ter a given value
                 }
            }
        }
   }

var testMap = new Map(3,3) //create a 3x3 map of hexes
testMap.step1();           //seed the map
testMap.step2();           //assign terrain values to neighbors of seeded hexes. get error.

My problem is that I get the following error when compiling: Uncaught TypeError: Cannot read property 'col' of undefined

I've experimented a lot with the step1 and getNeighbor functions, and they return accurate arrays of Hexes, so I don't think those are the problem.

I can't figure out what the problem is. Is there a problem with calling the getNeighbor function within the step2 function? for some reason it doesn't like

var temp_col = this.neighborArray[i].col;
4
  • 3
    Clearly this.neighborArray[i] has no col property, but you're not showing us how that array is created, or what it looks like ? Commented Nov 27, 2014 at 17:24
  • Where do you define this.step1Array? Commented Nov 27, 2014 at 17:25
  • @adeneo You're right - and even more clearly, it's because this.neighbourArray[i] is undefined. There must have been an error in the logic such that not all cells of that array have been populated by the time of the call. (And this is why I like the functional approach and eschew mutable state, because it's impossible to tell now who "should have" populated this array...) Commented Nov 27, 2014 at 17:27
  • Probably we don't need to see your business logic, limit your code samples to the bare minimum necessary to reproduce the problem. Commented Nov 27, 2014 at 17:35

1 Answer 1

1

It may be that your 'this' is not the 'this' you think it is as you are in an inner function.

You could try:

var _this = this;  //Create a local variable containing this
this.step2 = function() { //assigns a terrain value to all "blank" neighbors of the seeded hexes
    for (i=0; i<this.step1Array.length; i++) {
        _this.getNeighbor(this.step1Array[i]);  //use _this instead of this
        if (_this.neighborArray.length === 0) continue; 
Sign up to request clarification or add additional context in comments.

1 Comment

He's not in an inner function, he's in a method of the object.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.