# \[AS 2.0\] Implementing flood fill algorithm - Recursive problem

**URL:** <https://forum.kirupa.com/t/as-2-0-implementing-flood-fill-algorithm-recursive-problem/290870>\
**Category:** flash\
**Created:** [June 21, 2009, 11:59am UTC](https://forum.kirupa.com/t/as-2-0-implementing-flood-fill-algorithm-recursive-problem/290870 "2009-06-21T11:59:42Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![Arti\_Ravnos](https://avatars.discourse-cdn.com/v4/letter/a/d07c76/32.png) [@Arti\_Ravnos](https://forum.kirupa.com/u/Arti_Ravnos)\
**Post date:** [June 21, 2009, 11:59am UTC](https://forum.kirupa.com/t/as-2-0-implementing-flood-fill-algorithm-recursive-problem/290870/1 "2009-06-21T11:59:42Z")

</div>

I’m making a level editor for a game, but this question doesn’t have to be game related so I decided to post it here. I’m using Actionscript 2.0 (Flash 8)

I’m trying to implement the [[COLOR=#810081]flood fill algorithm[/COLOR]](http://en.wikipedia.org/wiki/Floodfill#The_algorithm) so that I can paint tiles just like the paint bucket in mspaint.

The level consist of a BitmapData object which is 2880px in width and 2176px in height.

Here’s my code:

```auto
function floodFill(x:Number, y:Number, targetTile:Number, replaceTile:Number)
{
 var curTile:Number = map[y][x];
 
 if (curTile != targetTile || curTile == replaceTile)
  return;
 
 paintTile(x, y);
 
 if (x > 0)
   floodFill(x - 1, y, targetTile, replaceTile);
 
 if (x < levelW/tileW - 1)
  floodFill(x + 1, y, targetTile, replaceTile);
 
 if (y > 0)
  floodFill(x, y - 1, targetTile, replaceTile);
 
 if (y < levelH/tileH - 1)
  floodFill(x, y + 1, targetTile, replaceTile);
 
}

```

The function works great for smaller areas, but when I try to use the paint bucket on the entire map I get this error:

```auto
256 levels of recursion were exceeded in one action list.
This is probably an infinite loop.
Further execution of actions has been disabled in this movie.

```

I hope somebody hear can help me out. 🙂
