Take the 2-minute tour ×
Game Development Stack Exchange is a question and answer site for professional and independent game developers. It's 100% free, no registration required.

Hi when I call the the function below in the chrome console it tells me "Type error cannot read property '2' of undefined" why?.

defining the map:

var map = {
FALL:[
    [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
    [0, 0, 0, 0, 0, 4, 0, 0, 0, 0, 0],
    [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
    [3, 0, 0, 2, 0, 0, 0, 3, 0, 0, 3]
    ],
}

Function:

    for (i = 0; i < 4; i++) {
    console.log(map.FALL[pmx-i]
    [pmy-1]);
    if(map.FALL[pmx-i][pmy-1] > 0){
        return true;
        break;
    }

by the way pmx = 1 pmy=3

share|improve this question

closed as off-topic by Seth Battin, Anko, Byte56 Jun 9 at 22:28

This question appears to be off-topic. The users who voted to close gave this specific reason:

  • "Programming questions that aren't specific to game development are off-topic here, but can be asked on Stack Overflow. A good rule of thumb is to ask yourself "would a professional game developer give me a better/different/more specific answer to this question than other programmers?"" – Seth Battin, Anko, Byte56
If this question can be reworded to fit the rules in the help center, please edit the question.

    
This is easy to debug by adding console.log(pmx-i) before the failing line, or using Chrome's debugging tools. –  Anko Jun 7 at 10:42

1 Answer 1

up vote 1 down vote accepted

In the third iteration of the loop, pmx-i is equal to -1, map.FALL[-1] is undefined and undefined[pmy-1] is an error since you can't access an index of undefined.

share|improve this answer

Not the answer you're looking for? Browse other questions tagged or ask your own question.