1

I have a problem. I have a sprite added to library and I linked and named its class "CarriedHead_Normal". Now I want to display this sprite and make every black(000000) pixel invisible (just convert background to alpha).

I know I can use this

 var bmpd:BitmapData = new BitmapData(width, height, true, 0x000000)

But how am I supposed to merge this with my function, so I call the class from the library and make it's bg transparent?

My current code:

 var imageSprite = new Sprite();
 addChild(imageSprite);



 var libraryImage:Bitmap = new Bitmap(new CarriedHead_Normal(0, 0))

 imageSprite.addChild(libraryImage);

Thanks in advance.

Gad
  • 23
  • 4

2 Answers2

0

Try something like:

var libraryImage:Bitmap = spriteToBitmap(new CarriedHead_Normal());
imageSprite.addChild(libraryImage);

function spriteToBitmap(sprite:Sprite, smoothing:Boolean = false):Bitmap
{
   var bitmapData:BitmapData = new BitmapData(sprite.width, sprite.height, true, 0x00000000);
   bitmapData.draw(sprite);
   bitmapData.threshold(bitmapData, bitmapData.rect, new Point(), '==', 0xff000000, 0x00000000);
   return new Bitmap(bitmapData, "auto", smoothing);
}
gabriel
  • 2,351
  • 4
  • 20
  • 23
0

A number of ways you could do it, some working better than others:

On a side node, your question title has nothing to do with your actual question

1) Use blend modes

Depending on your graphics and background, you might be able to get away with a simple blendMode - check out BlendMode.SCREEN in particular. Note that some blend modes require that your parent container have a blendMode of BlendMode.LAYER

2) copyPixels() using an alpha BMD

copyPixels() lets you specify an alpha BitmapData to use when deciding which pixels to copy over (actually the alpha value of the pixels to copy over). If you have an alpha BitmapData that matches your black areas, you can remove it like that:

var b:BitmapData = new BitmapData( myImageBMD.width, myImageBMD.height, true, 0x00000000 );
b.copyPixels( myImageBMD, myImageBMD.rect, new Point, alphaBMD, new Point, false ); // copy our image, using alphaBMD to remove the black

http://help.adobe.com/en_US/FlashPlatform/reference/actionscript/3/flash/display/BitmapData.html#copyPixels()

3) floodFill() the black areas

If your black area is continuous and you know a pixel that it starts on (e.g. it's a black frame around an image), then you can use floodFill() to remove the black areas

var b:BitmapData = new BitmapData( myImageBMD.width, myImageBMD.height, true, 0x00000000 );
b.copyPixels( myImageBMD, myImageBMD.rect, new Point ); // copy our image so we keep the original
b.floodFill( 0, 0, 0x00000000 ); // assuming the first pixel is black, floodFill() from here, replacing with transparence

http://help.adobe.com/en_US/FlashPlatform/reference/actionscript/3/flash/display/BitmapData.html#floodFill()

This technique will work best on pixel art, where the edges are well defined.

4) threshold() to remove the black

threshold() takes pixel values and replaces them with the value you set if they pass a certain test (<, <=, ==, etc). You can use this to replace black with transparence.

var b:BitmapData = new BitmapData( myImageBMD.width, myImageBMD.height, true, 0x00000000 );
b.copyPixels( myImageBMD, myImageBMD.rect, new Point ); // copy our image so we keep the original
b.threshold( b, b.rect, new Point, "==", 0xff000000, 0x00000000 ); // test the pixels; if they're black, replace with transparence

http://help.adobe.com/en_US/FlashPlatform/reference/actionscript/3/flash/display/BitmapData.html#threshold()

Again, this works best when the edges are well defined.

5) Use filters

You can use applyFilter() to create a new image, using a BitmapFilter to remove the black. Possibly only of the other filters will work, but ShaderFilter should definitely do the job (you write your own shader, so you can essentially do what you want). I don't have any real experience with filters, so I can't tell you how they work, but some googling should give you the necessary code

http://help.adobe.com/en_US/FlashPlatform/reference/actionscript/3/flash/display/BitmapData.html#applyFilter()

http://help.adobe.com/en_US/FlashPlatform/reference/actionscript/3/flash/filters/ShaderFilter.html

6) Use photoshop (or equivalent)

The easiest method; just edit your image to remove the black; much less hassle :)

divillysausages
  • 7,883
  • 3
  • 25
  • 39