# Tile array, finding neighbor tiles

7 posts

 817 posts How would I go about finding out if a tile has another tile next to it? For example, the below has 1’s that instantiate a collide-able tile later on, while the 0’s instantiate nothing. The below is arrays within an array if thats not obvious. How do I find out if a 1 has a 1 next to it? I plan on than telling the application to give the checked 1 and boolean value of true for checked. And hopefully adding the neighbor tiles and direction they are in to a mini array or vector of each personal tile for quick access to whether it has a neighbor or not. Maneuvering in multiple dimension arrays is quite confusing. ```public var map_array:Array = new Array(); public function setup(){ map_array = [ [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1], [1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1], [1, 0, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1], [1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1], [1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1], [1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 0, 0, 0, 0, 0, 1], [1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1], [1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1], [1, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1], [1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1], [1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1], [1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1], [1, 0, 0, 0, 0, 0, 0, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1], [1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1], [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1] ] ``` 431 posts if tile[x-1][y] is 0, space to the left, otherwise collide to the left, and so on? 64 posts if your map is stored in a multidimensional array like this `map_array[x][y]` then you can just do a check for each side like ``` function hasWellNexTo(x_:int, y_:int):Boolean { var found:Boolean = false; for (var i:int = 0; i < 5; i++) { if (i == 0) { if (map_array[x + 1][y] == 1) { found = true; } } if (i == 1) { if (map_array[x][y + 1] == 1) { found = true; } } if (i == 2) { if (map_array[x][y – 1] == 1) { found = true; } } if (i == 3) { if (map_array[x – 1][y] == 1) { found = true; } } if (found) { return true; } } return false; }``` but I don’t believe that is the most efficient way 817 posts Yeah, I’m doing this because I’m trying to find when a collision is inappropriate. o 111 with the o being the player, it would be inappropriate to collide with the middle tiles left and right sides, with the left tiles right side and the right tiles left side. 271 posts ```function getAdjacentTiles(tx:int, ty:int):Array { var adjacentTiles:Array = new Array(); for (var i:int = -1; i <= 1; i++) { for (var j:int = -1; j <= 1; j++) { adjacentTiles.push(mapList[tx+i][ty+j]); } } return adjacentTiles; } ``` That will return all 9 neighbouring tiles to the tile at p{tx, ty} Also just remember to make sure your tiles tx+i and ty+j are in the bounds of your map array, or you might get some null values if your on the edge of your map. 431 posts 8 neighbouring tiles 271 posts LOL, yes i meant 8 neighboring tiles. But that code would include the tile your on… so just make sure to add bound checks and make sure x and y are not 0.