Actionscript 3 AS3:位图数据链接到库中的特定精灵?

Actionscript 3 AS3:位图数据链接到库中的特定精灵?,actionscript-3,actionscript,background,sprite,bitmapdata,Actionscript 3,Actionscript,Background,Sprite,Bitmapdata,我有个问题。我在库中添加了一个精灵,我链接并命名它的类CarriedHead_Normal。现在我想显示这个精灵,让每一个black000000像素都不可见,只需将背景转换为alpha即可 我知道我可以用这个 var bmpd:BitmapData = new BitmapData(width, height, true, 0x000000) 但我应该如何将其与函数合并,以便从库中调用该类并使其bg透明 我当前的代码: var imageSprite = new Sprite(); ad

我有个问题。我在库中添加了一个精灵,我链接并命名它的类CarriedHead_Normal。现在我想显示这个精灵,让每一个black000000像素都不可见,只需将背景转换为alpha即可

我知道我可以用这个

 var bmpd:BitmapData = new BitmapData(width, height, true, 0x000000)
但我应该如何将其与函数合并,以便从库中调用该类并使其bg透明

我当前的代码:

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



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

 imageSprite.addChild(libraryImage);
提前谢谢。

试试以下方法:

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);
}

您可以通过多种方式实现这一目标,其中一些方法比其他方法更有效:

在侧节点上,问题标题与实际问题无关

1使用混合模式

根据您的图形和背景,您可能可以使用简单的blendMode,尤其是查看blendMode.SCREEN。请注意,某些混合模式要求父容器的blendMode为blendMode.LAYER

使用alpha BMD复制2个像素

copyPixels允许您指定alpha位图数据,以便在决定要复制哪些像素时使用,实际上是要复制的像素的alpha值。如果您有一个与黑色区域匹配的alpha BitmapData,您可以这样删除它:

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
3洪水淹没了黑色区域

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
如果您的黑色区域是连续的,并且您知道它从一个像素开始,例如,它是图像周围的黑色帧,那么您可以使用泛光填充来移除黑色区域

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
这项技术在像素艺术上效果最好,因为边缘是很好定义的

4清除黑色的阈值

threshold接受像素值,如果它们通过某个测试,则用您设置的值替换它们