0

Error: Uncaught TypeError: Cannot set property '0' of undefined

relevant code:

var field = new Array(6);
for(var x=0; x<field.length; x++){
  field[x] = new Array(12);
  for(var y=0; y<field[x].length; y++){
    field[x][y]= new tile();
    field[x][y].type = Math.floor(Math.random()*7);
    field[x][y].subtype[0]=false;//error happens when I run this line
    field[x][y].subtype[1]=false;
    field[x][y].subtype[2]=false;
    field[x][y].subtype[3]=false;
  }
}


function tile() {
  var type = 0; type = 0;//int
  var subtype = new Array(4);//Boolean

//+ some functions
}

I know that I am missing something, but I don't know what. Any help?

edit: also, when I added "this." before 'type' and 'subtype' in the tile object, I got Uncaught SyntaxError: Unexpected token this edit#2: I got that error because I typed

var this.
//instead of
this.
3
  • What do you expect: var type = 0; type = 0; to do? You do realize the var declaration is already setting the value to 0. Commented Apr 1, 2014 at 2:48
  • By convention, functions used as constructors have a name that starts with a capital letter, so ... = new Tile(); stands out more. Commented Apr 1, 2014 at 3:11
  • To izuriel: It fixed this issue when I was working with just type, but the same method could not be applied to subType. /n To RobG: thank you for telling me, this is my first "teach yourself js" project, and pointers on convention are very good Commented Apr 1, 2014 at 3:15

2 Answers 2

3

You need to set your tile properties as just that, member properties by prefixing them with the this keyword, ie

function tile() {
    this.type = 0;
    this.subtype = new Array(4);
}
Sign up to request clarification or add additional context in comments.

Comments

1

Just a comment:

To save a lot of typing, you might consider something like:

for(var y=0; y<field[x].length; y++){
  field[x].push(new Tile(Math.floor(Math.random()*7)));
}

and in the constructor:

function Tile(type, subtypes) {
  this.type = type;
  this.subtype = subtypes || [false,false,false,false];
}

so that a new Tile can be initilised with values passed in the call or set to defaults if not.

2 Comments

The first suggestion sounds good, but will I later be able to refer to individual tiles as field[x][y]? The second suggestion is impracticable, as the code that I posted is to create a scenario that would otherwise be created by logic in the Tile object, but not at initialization. This is only so that I can see how they will act while designing the engine. how does the first suggestion work, so that I might use it later?
@user3483402—in regard to "...able to refer to individual tiles..", yes. It builds exactly the same structure, it just doesn't use y. In regard to the initialiser, it allows passing the "subtype" array when calling the constructor, e.g. new Tile(type, [false,false,false,false]) so you can generate the subtype array however and assign it later. If you can't do that, keep going as you are. :-)

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.