view raw
1.21 gigawatts 1.21 gigawatts - 6 months ago 53
ActionScript Question

How do you detect an area that can be filled in in a bitmap in ActionScript 3?

I need to be able to detect a fill area for something similar to a coloring book picture. The user will click inside the area that needs to be filled. The image is user created bitmap content and so the fill area must be detected at runtime.

protected function myImage_clickHandler(event:MouseEvent):void

<s:Image id="myImage" click="myImage_clickHandler(event)" source="/images/square.gif"/>


mouseX and mouseY are always the mouse position add a click listener to whatever object and just use the mouseX/mouseY or else the MouseEvent's event.localX and event.localY for the object that the listener is attached to

<mx:Image id="myImage" click="(myImage.content as Bitmap).bitmapData.floodFill(event.localX,event.localY,0xFFFFFF)" source=""/>

Here's a solution if you use the Flex 4.5 Hero framework otherwise I was unable to get a handle on the bitmapData through the BitmapImage, the above solution also still works with Flex 3 or 4:

<?xml version="1.0" encoding="utf-8"?>
<s:Application xmlns:fx="" 
               xmlns:mx="library://" minWidth="955" minHeight="600" xmlns:local="*">
        <!-- Place non-visual elements (e.g., services, value objects) here -->

        click="(myImage.content as Bitmap).bitmapData.floodFill(event.localX,event.localY,0xFFFFFF)"
            private function handleClick(event:MouseEvent):void
                var tempData:BitmapData = new BitmapData(myBitmapImage.width,myBitmapImage.height);
                tempData = myBitmapImage.bitmapData;

                var localPoint:Point = container.globalToLocal(new Point(mouseX,mouseY));
                myBitmapImage.source = tempData;
                trace(localPoint.x + ":" + localPoint.y);
            source="@Embed('square.gif')" />


even though they exposed the bitmapData in the 4.5 framework it's read only though so I had to copy it and re-assign it to the source that seemed to work