Okay, so I'm working on some procedural dungeon generation code. I have a createTunnel function that randomly selects a starting point (randomY , randomX), then loops a set number of times through a routine that (1) creates an array with the four cardinal directions, (2) goes through and culls directions that lead to an already-existing room or off the end of the array, (3) randomly selects from the remaining directions, and (4) adds a room there, resetting randomX and randomY to the new coordinates for the next cycle.
Here's the problem: whenever Flash checks a direction that would lead off the end of the array as part of step 2 of the routine, it throws this error at me:
Error #1010: A term is undefined and has no properties. Here is the relevant code:
var tunnelDir:Array = ["left" , "right" , "up" , "down"]; //create array of possible directions
var newY:int = randomY + 1; //check below;
if ( floorArray[newY][randomX] != 0 || newY > floorArray.length ) { //if the space is a room or off the map
tunnelDir.splice( 3 , 1 ); //splice it from the array
}
newY = randomY - 1; //check above;
if ( floorArray[newY][randomX] != 0 || newY < 0 ) { //if the space is a room or off the map
tunnelDir.splice( 2 , 1 ); //splice it from the array
}
var newX:int = randomX + 1; //check right;
if ( floorArray[randomY][newX] != 0 || newX > floorArray[0].length ) { //if the space is a room or off the map
tunnelDir.splice( 1 , 1 ); //splice it from the array
}
newX = randomX - 1; //check left;
if ( floorArray[randomY][newX] != 0 || newX < 0 ) { //if the space is a room or off the map
tunnelDir.splice( 0 , 1 ); //splice it from the array
}
I tried recoding it so it won't try to return a value for newY or newX that extend past the edges of the array, but I still get the same error anyway:
var newY:int = randomY + 1; //check below;
if ( newY > floorArray.length ) { //if the space is off the map
tunnelDir.splice( 3 , 1 ); //splice it from the array
}
else if ( floorArray[newY][randomX] != 0 ) { //if the space is a room
tunnelDir.splice( 3 , 1 ); //splice it from the array
}
newY = randomY - 1; //check above;
if ( newY < 0 ) { //if the space is off the map
tunnelDir.splice( 2 , 1 ); //splice it from the array
}
else if ( floorArray[newY][randomX] != 0 ) { //if the space is a room
tunnelDir.splice( 2 , 1 ); //splice it from the array
}
//etc.
I assume that there's some principle here I'm missing. Does anyone know how I can get around this problem?