Problem:
When I get to the end of the Grid it gives me an Error :-/
The X axis is fine, the problems occur then I move "over the end" of TOP and BOTTOM.
This code works:
if(new_data[1] == "right"){
// If New Grid Location is Empty
if(grid[player_y][player_x + 1] == 0){
grid[player_y][player_x] = 0;
player_x = player_x + 1;
grid[player_y][player_x] = 1;
socket.emit("message", "move," + "right");
}
And this one does not:
if(new_data[1] == "up"){
// If New Grid Location is Empty
if(grid[player_y - 1][player_x] == 0){
grid[player_y][player_x] = 0;
player_y = player_y - 1;
grid[player_y][player_x] = 1;
socket.emit("message", "move," + "up");
It says the error is "Cant == 0 of Undefined"
I assume that the Problem lies in the First Array being less then 0 and therefore giving a Undefined Error on the Second Array..
I fixed this by creating a "Wall" at the X Edges:
for(var i = 0; i < 20; i++){
grid[0][i] = 1;
}
for(var i = 0; i < 20; i++){
grid[19][i] = 1;
}
But why does it not give an Error when I try to access a "undefined" of the Second Array?