[AS 2.0] Implementing flood fill algorithm - Recursive problem

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] 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:

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:

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. :slight_smile: