Flash flood fill implementation

Do you know what is the flood fill algorithm?

From Wikipedia: Flood fill, also called seed fill, is an algorithm that determines the area connected to a given node in a multi-dimensional array. It is used in the "bucket" fill tool of paint programs to determine which parts of a bitmap to fill with color, and in puzzle games such as Minesweeper, Puyo Puyo, Lumines, and Magical Drop for determining which pieces are cleared.

So now you know there is an algorithm to manage some issues in "click somewhere and..." games.

Let's make it!

My version of the algorithm looks for all light grey tiles (two-dimensional array elements) which are connected to the starting light grey tile (the one we click) by a path of same tiles, and changes them to pink.

It's a ricorsive algorithm acting this way: if the clicked tile is grey, then turn it into pink, then act as we clicked the tile at the top of the clicked tile, the one at the bottom, the one at the left and the one and the right. This is called 4-directions flood fill, because we extend the flooding to four directions (up, down, left, right).

You can also perform a 8-directions flood fill, extending the flooding to the original four directions plus the four diagonals

Look at this actionscript:

ACTIONSCRIPT:
  1. fill_map = new Array();
  2. fill_map[0] = [1, 1, 1, 1, 1, 1, 1, 1, 1];
  3. fill_map[1] = [1, 0, 0, 0, 1, 0, 0, 0, 1];
  4. fill_map[2] = [1, 0, 0, 0, 1, 0, 0, 0, 1];
  5. fill_map[3] = [1, 0, 0, 1, 0, 0, 0, 0, 1];
  6. fill_map[4] = [1, 1, 1, 0, 0, 0, 1, 1, 1];
  7. fill_map[5] = [1, 0, 0, 0, 0, 1, 0, 0, 1];
  8. fill_map[6] = [1, 0, 0, 0, 1, 0, 0, 0, 1];
  9. fill_map[7] = [1, 0, 0, 0, 1, 0, 0, 0, 1];
  10. fill_map[8] = [1, 1, 1, 1, 1, 1, 1, 1, 1];
  11. for (y=0; y<9; y++) {
  12.     for (x=0; x<9; x++) {
  13.         tile = _root.attachMovie("tile", "tile_"+(y+x*9), _root.getNextHighestDepth(), {_x:y*50, _y:x*50});
  14.         tile.gotoAndStop(1+fill_map[x][y]);
  15.     }
  16. }
  17. _root.attachMovie("cursor", "cursor", _root.getNextHighestDepth());
  18. cursor.onEnterFrame = function() {
  19.     this._x = 50*Math.floor(_root._xmouse/50);
  20.     this._y = 50*Math.floor(_root._ymouse/50);
  21. };
  22. _root.onMouseDown = function() {
  23.     pos_x = Math.floor(_root._xmouse/50);
  24.     pos_y = Math.floor(_root._ymouse/50);
  25.     flood_fill(pos_x, pos_y);
  26. };
  27. function flood_fill(x, y) {
  28.     pos = x+y*9;
  29.     if (fill_map[y][x] == 0) {
  30.         fill_map[y][x] = 2;
  31.         _root["tile_"+(x+y*9)].gotoAndStop(3);
  32.         flood_fill(x+1,y);
  33.         flood_fill(x-1,y);
  34.         flood_fill(x,y+1);
  35.         flood_fill(x,y-1);
  36.     }
  37. }

The 4-directions flood function goes from line 27 to line 37 and allows me to paint grey areas this way:

As you can see, you can paint only one "room" at time because the diagonal "walls" does not allow the four direction flood to paint everywhere.

Look at the 8-directions flood:

ACTIONSCRIPT:
  1. fill_map = new Array();
  2. fill_map[0] = [1, 1, 1, 1, 1, 1, 1, 1, 1];
  3. fill_map[1] = [1, 0, 0, 0, 1, 0, 0, 0, 1];
  4. fill_map[2] = [1, 0, 0, 0, 1, 0, 0, 0, 1];
  5. fill_map[3] = [1, 0, 0, 1, 0, 0, 0, 0, 1];
  6. fill_map[4] = [1, 1, 1, 0, 0, 0, 1, 1, 1];
  7. fill_map[5] = [1, 0, 0, 0, 0, 1, 0, 0, 1];
  8. fill_map[6] = [1, 0, 0, 0, 1, 0, 0, 0, 1];
  9. fill_map[7] = [1, 0, 0, 0, 1, 0, 0, 0, 1];
  10. fill_map[8] = [1, 1, 1, 1, 1, 1, 1, 1, 1];
  11. for (y=0; y<9; y++) {
  12.     for (x=0; x<9; x++) {
  13.         tile = _root.attachMovie("tile", "tile_"+(y+x*9), _root.getNextHighestDepth(), {_x:y*50, _y:x*50});
  14.         tile.gotoAndStop(1+fill_map[x][y]);
  15.     }
  16. }
  17. _root.attachMovie("cursor", "cursor", _root.getNextHighestDepth());
  18. cursor.onEnterFrame = function() {
  19.     this._x = 50*Math.floor(_root._xmouse/50);
  20.     this._y = 50*Math.floor(_root._ymouse/50);
  21. };
  22. _root.onMouseDown = function() {
  23.     pos_x = Math.floor(_root._xmouse/50);
  24.     pos_y = Math.floor(_root._ymouse/50);
  25.     flood_fill(pos_x, pos_y);
  26. };
  27. function flood_fill(x, y) {
  28.     pos = x+y*9;
  29.     if (fill_map[y][x] == 0) {
  30.         fill_map[y][x] = 2;
  31.         _root["tile_"+(x+y*9)].gotoAndStop(3);
  32.         flood_fill(x+1,y);
  33.         flood_fill(x+1,y+1);
  34.         flood_fill(x+1,y-1);
  35.         flood_fill(x-1,y);
  36.         flood_fill(x-1,y+1);
  37.         flood_fill(x-1,y-1);
  38.         flood_fill(x,y+1);
  39.         flood_fill(x,y-1);
  40.     }
  41. }

Now you can paint everywhere

What's better? The one that fits your needs, of course?

What kind of game can we make out of that? I don't know (lie! lie!), it's up to you

Download the source code and start flooding

Rate this post: 1 Star2 Stars3 Stars4 Stars5 Stars (1 votes, average: 4 out of 5)
Loading ... Loading ...
If you found this post useful, please consider a small donation.
» Flash Templates provided by Template Monster are pre-made web design products developed using Flash technology.
They can be easily customized to meet the unique requirements of your project.

11 Responses to “Flash flood fill implementation”

  1. David on June 6th, 2008 6:50 pm

    interesting post
    i might use it
    when are you going to do the next part of the creation of a ragdoll with flash tutorial?

    nice tutorial though

  2. EagleVision on June 6th, 2008 7:49 pm

    Its very nice, but at first I didn’t know what it was… If you ask me, no Ideas. :)

    Yeah, thats a problem with me blogging, too. I forget about the old post that I was suppose to continue, but didn’t.

    I’ll probably need some encouragement on that. :)

  3. Mike on June 6th, 2008 7:56 pm

    It’s simple, but has to be optimized a lot. In 1st example if I click big center area, algorithm invokes “flood_fill” function 93 times. It’s way too much to fill up 23 tiles.

  4. Jack Hopkins on June 6th, 2008 10:51 pm

    Awesome!
    This is quite advanced I feel,
    I think I can use this!

    :D

  5. Cory Mathews on June 7th, 2008 12:31 am

    Excellent article. I see many uses for this.

  6. Junxron on June 7th, 2008 4:01 am

    OMG.. Last night I was thinking about how 2 do “snap to grid” kind of games.. And this morning i wake up i see this post.. LOL.. Thanks..

  7. Chuck Arellano on June 7th, 2008 8:06 pm

    Hi Emanuele,

    Another game that uses flood fill is JezzBall! I wrote a version of the 4 directions flood fill you mentioned above with my newest game, Trappit ( http://www.scriptedfun.com/games/trappit/ ), but I implemented it non-recursively.

    Great tutorial as always! Thank you! :D

  8. Adam Owen on June 9th, 2008 2:27 pm

    Last year I was making a bomb game where I had to recursively check 4-way tiles to see if they themselves had bombs.

    I have to say, my method was extremely sloppy and this article would have saved me a lot of headache.

    Great article Emanuele, and an interesting topic!

  9. Adarsh Sinha on June 10th, 2008 4:10 pm

    Hello Emanuele Feronato, I have been following your blog for quite some time now, And i was wondering if you could make a tutorial for a small online game, more or less with a goal to show how to make a small, simple mmo/online game in flash.
    Thanks :)

  10. canberk on December 5th, 2008 12:39 pm

    it is reaally a perfect code. but i couldn’t decide whether recursion is used in that code segment. If someone could answer, i’ll be thankful…
    Thanks for sharing of that nice code…

  11. koby on July 2nd, 2009 2:02 pm

    I made it on AS3 today.
    this is a great tut.

Leave a Reply




Posts