I have a Sprite that contains opaque objects surrounded by transparent pixels. I also have a Rectangle that represents a part of the Sprite that I want to render using BitmapData.draw().
The problem is that the Rectangle that I obtained also contains transparent parts. How can I narrow the Rectangle to only include the opaque parts and ignore the transparent surroundings?
kapep’s answer is just what I want to do, but the problem is that the source is a Sprite, not a BitmapData object, and I don’t have getColorBoundsRect() on a Sprite.
You can use
getColorBoundsRectfor this.To operate with the pixels of the sprite, it needs to be drawn to a
BitmapDatafirst (useBitmapData.draw). Then after getting a smaller rectangle withgetColorBoundsRect, create a new BitmapData with the dimensions of that rectangle. The last step is to use copyPixels, to copy the area of the rectangle from first image to the new one.