I have a matrix as arena and a small matrix as player. With keys user can move around. move(pos) takes argument and inside checks
if(pos=="down")
if (!collide(pos)) player.pos.y++;
And collide works like
if(pos=="down")
for (i = 0; i < player.matrix.length; i++)
{
for (j = 0; j < player.matrix[i].length; j++)
{
if ((player.matrix[i][j] && arena[player.pos.y + i + 1][player.pos.x + j]) != 0) result = true;
}
}
This woks perfect till you come edge to the arena. When you come to edge, because there is no lenght+1 index, if detection gives you Cannot read property of "" undefined. I tried first checking if typeof index out of bound == undefined however even this gave me the same error. Then I tried try catch but the next function inside the catch stuck between first one and itself. Any ideas to find an easy solution for checking out of bonds?
if(item[index] && ... ). What will happen is any non-existent item will immediately be false on this first condition.