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
    
This is easy to debug by adding console.log(pmx-i) before the failing line, or using Chrome's debugging tools. –  Anko yesterday

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

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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