Quantcast
Viewing all articles
Browse latest Browse all 26

Flood Fill algorithm implementation in C# problem

Hi i am trying to implement flood fill algorithm version 3 from Wiki [here][1] and I cant seem to get it right. I have a main method called flood fill and another helper method to get the game Object from a given position. My queue is of gameObject and so are my nodes. I can not seem to find out Why some times some of the tiles get deleted and i get null object reference error. lemme show you. ![alt text][2] ![alt text][3] Here when in the first image i click i get the next image where the tile at 0,0 gets deleted and then the next click i do gives me NullReferenceException: Object reference not set to an instance of an object. This error does not nesacary comes up on the 3rd click but sometimes the floodfill method works for alot of clicks before i get a deleted tile and then eventually i get the error. Can anyone please tell me whats going on. Thanks for any help. here is the code public void floodFill (GameObject node, Color targetColor, Color replacementColor) { Queue Q = new Queue (); if (node.GetComponent ().color != targetColor) return; Q.Enqueue (node); GameObject w, e, y; while (Q.Count > 0) { GameObject n = Q.Dequeue (); if (FindGameObjectFromPos (new Vector3 (n.transform.position.x, n.transform.position.y)).GetComponent ().color == targetColor) { w = n; e = n; y = n; while (((w.transform.position.x) > 0) && FindGameObjectFromPos (new Vector2 (w.transform.position.x - 1, w.transform.position.y)).GetComponent ().color == targetColor) { //FindGameObjectFromPos (new Vector3 (w.transform.position.x, w.transform.position.y)).GetComponent ().color = replacementColor; w.transform.position = new Vector3 (w.transform.position.x - 1, w.transform.position.y, w.transform.position.z); } while (((e.transform.position.x < 18) && FindGameObjectFromPos (new Vector2 (e.transform.position.x + 1, e.transform.position.y)).GetComponent ().color == targetColor)) { //FindGameObjectFromPos (new Vector3 (e.transform.position.x , e.transform.position.y)).GetComponent ().color = replacementColor; e.transform.position = new Vector3 (e.transform.position.x + 1, e.transform.position.y, e.transform.position.z); } for (int i = (int)w.transform.position.x; i <= (int)e.transform.position.x; i++) { //x.transform.position = new Vector2 (i, e.transform.position.y); FindGameObjectFromPos (new Vector3 (i, y.transform.position.y)).GetComponent ().color = replacementColor; if (((y.transform.position.y) > 0) && FindGameObjectFromPos (new Vector2 (i, y.transform.position.y - 1)).GetComponent ().color == targetColor) { Q.Enqueue (FindGameObjectFromPos (new Vector2 (i, y.transform.position.y - 1))); } if (((y.transform.position.y) < 18) && FindGameObjectFromPos (new Vector2 (i, y.transform.position.y + 1)).GetComponent ().color == targetColor) { Q.Enqueue (FindGameObjectFromPos (new Vector2 (i, y.transform.position.y + 1))); } } } } } GameObject FindGameObjectFromPos (Vector2 pos) { GameObject go; Collider2D goc = Physics2D.OverlapPoint (pos); go = goc.gameObject; Debug.Log ("go " + goc.gameObject.GetComponent ().color); return go; } [1]: https://en.wikipedia.org/wiki/Flood_fill [2]: /storage/temp/66403-capture.png [3]: /storage/temp/66404-capture1.png

Viewing all articles
Browse latest Browse all 26

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>