Search code examples
actionscript-3bitmapbitmapdata

FloodFill in AS3


I have been making a basic painting application similar to MS-Paint with basic paint, eraser and fill tools. It's this last one that's giving me some trouble.

I'm pretty new to using BitmapData but the idea is that when the user clicks the board, it triggers the startFloodFill method. This is shown below:

public static function startFloodFill(e:MouseEvent):void
    {
        trace("FLOODFILL");
        var boardRef:MovieClip = e.currentTarget.parent.board;                      //Creates a reference to the board
        var boardData:BitmapData = new BitmapData(boardRef.width, boardRef.height); //Creates a new BitmapData with the same size as boardRef
        boardData.floodFill(e.localX, e.localY, 0x00CCCCCC);                        //Applies the FloodFill
        boardData.draw(boardRef);                                                   //Saves the boardRef as bitmapData
        boardRef.bitmapData = boardData;                                            //Updates the board
            boardRef.parent.addChild(boardRef);
    }

Can anybody tell me what I've done wrong here? When I click, the board does not change. I expected the FloodFill to fill the entire bitmap with the chosen colour as the board is blank when I click.

I also tried replacing the last two lines with:

boardRef.addChild(new Bitmap(boardData) );                              //Updates the board

Thanks


Solution

  • The problem is that you first use the floodFill, and then use the draw method, which actually fills the BitmapData with whatever param you give it - in your case it's the boardRef.

    You should first draw the boardRef into the boardData, and then use floodFill. At the end, you need to create new Bitmap and display it.

    Now you are setting the bitmapData of a MovieClip?! Don't know what you wanted, but you just need to add new child (new Bitmap)