- Home /
Pathfinding. Does not "walk" on every tile.
Hello,
I'm working on a pathfinder of type A*, but I'm having some trouble trying to iterate each move.
Here's the code I've got so far:
while (tilesForSearching.length > 0) //While there is tiles to be searched...
{
for(var tile : GameObject in tilesForSearching) //...do this to each one...
{
var refTile = tile.GetComponent.<IndividualTileScript>(); //Reference for convenience.
refTile.state = 2; //...state of the tile is set to 2 (searching)...
tile.GetComponent.<SpriteRenderer>().color = Color.yellow; //Turns it yellow when it has been walked over.
//...all adjacent tiles is added to tilesForSearching...
if(refTile.adjacentTile_upper && refTile.adjacentTile_upper.GetComponent.<IndividualTileScript>().state == 0)
{
tilesForSearching.Add(refTile.adjacentTile_upper);
}
if(refTile.adjacentTile_lower && refTile.adjacentTile_lower.GetComponent.<IndividualTileScript>().state == 0)
{
tilesForSearching.Add(refTile.adjacentTile_lower);
}
if(refTile.adjacentTile_right && refTile.adjacentTile_right.GetComponent.<IndividualTileScript>().state == 0)
{
tilesForSearching.Add(refTile.adjacentTile_right);
}
if(refTile.adjacentTile_left && refTile.adjacentTile_left.GetComponent.<IndividualTileScript>().state == 0)
{
tilesForSearching.Add(refTile.adjacentTile_left);
}
//...removes itself from tilesForSearching...
tilesForSearching.Shift();
}
}
It checks if the variable state is equal to 0, which means that it is open and walkable.
At the moment, every tile is set to 0/open.
So in theory every tile should become yellow, this is not the case.
For some reason the upper right tiles are not yellow.
I think the fault may be when it's trying to remove the tile from the array, but in all honesty - I have no idea why it's not working.
Answer by Macknooze · Jul 04, 2015 at 06:39 PM
I figured it out!
As I suspected it was because the tiles wasn't properly removed from the array. This is the new code:
while (tilesForSearching.length > 0) //While there is tiles to be searched...
{
for(var tile : GameObject in tilesForSearching) //...do this to each one...
{
var refTile = tile.GetComponent.<IndividualTileScript>(); //Reference for convenience.
refTile.state = 2; //...state of the tile is set to 2 (searching)...
tile.GetComponent.<SpriteRenderer>().color = Color.yellow;
//...all adjacent tiles' states becomes 2 = searching and add them to tilesForSearching...
if(refTile.adjacentTile_upper && refTile.adjacentTile_upper.GetComponent.<IndividualTileScript>().state == 0)
{
tilesForSearching_temporary.Add(refTile.adjacentTile_upper);
}
if(refTile.adjacentTile_lower && refTile.adjacentTile_lower.GetComponent.<IndividualTileScript>().state == 0)
{
tilesForSearching_temporary.Add(refTile.adjacentTile_lower);
}
if(refTile.adjacentTile_right && refTile.adjacentTile_right.GetComponent.<IndividualTileScript>().state == 0)
{
tilesForSearching_temporary.Add(refTile.adjacentTile_right);
}
if(refTile.adjacentTile_left && refTile.adjacentTile_left.GetComponent.<IndividualTileScript>().state == 0)
{
tilesForSearching_temporary.Add(refTile.adjacentTile_left);
}
//...adjacent tiles gets a copy of this tile's tilePath, and adds themselves...
//...removes itself from tilesForSearching...
//tilesForSearching.Shift();
}
tilesForSearching.Clear();
tilesForSearching = tilesForSearching.Concat(tilesForSearching_temporary);
tilesForSearching_temporary.Clear();
testing++;
}
I simply put them in a temporary array and made sure the main one was empty before adding the new ones. it works perfectly now!
Your answer

Follow this Question
Related Questions
array of boxes 2 Answers
array out of range in simple pathinder javascript 1 Answer
How can i show the texture in the array? 2 Answers
Push once then keep updating. 1 Answer
boolean issue 1 Answer