- Home /
The question is answered, right answer was accepted
How to delete rows in a flood fill game
Hi i want to delete rows that get filled by one color in a flood fill game. The problem is that when i delete a row it gives me the null object reference error. the error occurs since the node in floodFill method is the first tile on the grid. here is an image to show you which rows i mean to delete
Can anyone advise me any way to do this. I do not necessarily need code but a way to do it. here is my floodfill code
public void floodFill (Color replacementColor)
{
Color targetColor = node.GetComponent<SpriteRenderer>().color;
if (node.GetComponent<SpriteRenderer> ().color != targetColor)
return;
if (node.GetComponent<SpriteRenderer> ().color == replacementColor)
return;
Q.Enqueue (node);
while (Q.Count > 0) {
n = Q.Dequeue ();
Debug.Log ("n" + n.transform.position);
if (FindColorOfGameObjectFromPos (new Vector2 (n.transform.position.x, n.transform.position.y)) == targetColor) {
int w = (int)n.transform.position.x;
int e = (int)n.transform.position.x;
int y = (int)n.transform.position.y;
while ((w > 0) && FindColorOfGameObjectFromPos (new Vector2 (w - 1, y)) == targetColor) {
w--;
}
while (((e < 17) && FindColorOfGameObjectFromPos (new Vector2 (e + 1, y)) == targetColor)) {
e++;
}
for (int i = w; i <= e; i++) {
FindGameObjectFromPos (new Vector3 (i, y)).GetComponent<SpriteRenderer>().color = replacementColor;
if ((y > 0)) {
if (FindColorOfGameObjectFromPos (new Vector2 (i, y - 1)) == targetColor) {
Q.Enqueue (FindGameObjectFromPos (new Vector2 (i, y - 1)));
}
}
if ((y < rows - 1)) {
if (FindColorOfGameObjectFromPos (new Vector2 (i, y + 1)) == targetColor) {
Q.Enqueue (FindGameObjectFromPos (new Vector2 (i, y + 1)));
}
}
}
player.position = new Vector2 (e, y);
}
}
}
Here is the code that tries to delete the rows. This basically deletes the bottom row every time player moves 1 row up.
void UpdateGrid()
{
for (int x = 0; x < columns; x++) {
for (int y = 0; y < 1; y++) {
if(n.transform.position.x != x || n.transform.position.y != y)
{
tilesObject.Remove (FindGameObjectFromPos(new Vector2(x,y)));
gridPositions.Remove (new Vector3 (x, y, 0f));
Destroy (FindGameObjectFromPos (new Vector3 (x, y, 0f)));
}
}
}
}
@EvilTalk
Answer by Happy-Zomby · Mar 26, 2016 at 12:17 PM
Hi, I think you may be able to do something easier by assigning an ID to your tiles in function of the color when your script generates a color it also assign the ID to the script on the tile. Then adding a row manager game object. Your tiles will become children of the row manager and the row manager will cycle through all its children and check their ID and if all IDs are the same it will delete itself and the children. If you don't want to put a script on each tile for the ID - you could put an array on the row manager - an array of IDs of colors which you would update and cycle through that array - if all are equal - delete the row manager and children. what do you think? hope it helps
my problem is not deleting the rows but the error that comes forth from deleting them. The thing is the flood fill algorithm i am using requires information from the node that i have set as the first tile. so when i delete the row it gives me an error of null object reference, However I have solved my issue by moving node to player position and changing some limiting values in the algorithm.
Thx for the help tho. time to move to next problem :)
Follow this Question
Related Questions
Flood Fill algorithm implementation in C# problem 2 Answers
Block puzzle (1010) help 0 Answers
Applying the pathfinder component to 2d grids 1 Answer
2D Tilebased Floodfill lightning 1 Answer