对于Actionscript 3“绘图应用程序”,我希望能够选择纹理并设置它的透明度。 因此我尝试设置纹理的透明度。 但它不工作。as3 - 设置纹理alpha值
我做什么:
- 起初我用graphics.linestyle()来设置线的厚度和ALPHA值。
- 之后,我(a)加载png,(b)读取它的bitmapData和(c),然后在lineBitmapStyle中使用它。
结果:
当绘制线(与的moveTo,了lineTo等)的线路使用质感,但忽略了 “阿尔法”,这是设定线型。
我在做什么错?
myLoader.contentLoaderInfo.addEventListener(Event.COMPLETE, setTexture);
setTexture(e:Event):void
{
e.currentTarget.removeEventListener(e.type, arguments.callee);
//Try 1: Trying to set the Alpha-Trasparency with "lineStyle"-Command:
myDrawContainer.graphics.lineBitmapStyle(5, 0xFF0000, 0,6);
//Try 2: Trying to set the Alpha-Transparency by changing the Alpha-Value of the loaded content:
myLoader.content.alpha = 0.6;
//Getting the BitmapData of the Image:
BitmapDataOfMyTexture = Bitmap(LoaderInfo(e.target).content).bitmapData
//"Using" the TBitmapData as "Color/Texture" for my Drawing:
myDrawContainer.graphics.lineBitmapStyle(BitmapDataOfMyTexture);
//Test-Drawing:
myDrawContainer.graphics.moveTo(0, 0);
myDrawContainer.graphics.moveTo(500, 500); //-> RESULT: Textured Line WITHOUT Transparency!
}
结果:我得到它使用纹理,但是缺乏透明度线。
(更新)解决方案:(感谢DodgerThud)
设置/改变加载图像的alpha通道,您不使用 “线型” 可是......
创建一个新的ColorTransform对象
然后设置它的“alphaMultiplier” -attribute具体alpha通道
然后将此通过使用加载的BitmapData的“colorTransform”方法将新创建的colorTransform-Object添加到加载的BitmapData中。
但是:
这并不没有一个alpha通道或没有自己的alpha通道激活影像工作。降低Alpha通道时,这些图像只会变黑。在这种情况下,你必须这样做:- 起初,我创造新的BitmapData-对象与“新”,设置其加载的图像的宽度和高度,宽度和高度,并设置第三个参数为TRUE( =透明度:开)。所以你得到了一个具有ACTIVATED透明度的“容器”。
- 然后,您在此“容器” - 对象上使用“copyPixels”来填充LOADED BitmapData-Object的像素。
- 而在此之后,上述方法与“colorTransform” - 对象带来了预期的结果。
因此,这里的工作代码:
myLoader.contentLoaderInfo.addEventListener(Event.COMPLETE, setTexture);
setTexture(e:Event):void
{
e.currentTarget.removeEventListener(e.type, arguments.callee);
//Getting the BitmapData of the Image:
BitmapDataOfMyTexture = Bitmap(LoaderInfo(e.target).content).bitmapData
//Create an ADDITIONAL BitmapData-Object with 3rd
//argument set to TRUE and with same width and height
//as the LOADED image:
var BMDContainerWithAlphaActivated:BitmapData;
BMDContainerWithAlphaActivated = new BitmapData(BitmapDataOfMyTexture.width, BitmapDataOfMyTexture.height, true, 0xFFFFFF);
//Copy the pixels of the loaded image into the newly created
//"BitmapData-Container with activated AlphaChannel":
BMDContainerWithAlphaActivated.copyPixels(BitmapDataOfMyTexture, new Rectangle(0, 0, BitmapDataOfMyTexture.width, BitmapDataOfMyTexture.height), new Point(0,0))
//Modify the Alpha-Value (of the NEW BitmapData-Object):
var colorChanges:ColorTransform = new ColorTransform();
colorChanges.alphaMultiplier = 0.3;
BMDContainerWithAlphaActivated.colorTransform(new Rectangle(0, 0, BitmapDataOfMyTexture.width, BitmapDataOfMyTexture.height), colorChanges);
//"Using" the (NEW) BitmapData as "Color/Texture" for my Drawing:
myDrawContainer.graphics.lineBitmapStyle(BMDContainerWithAlphaActivated);
//Test-Drawing:
myDrawContainer.graphics.moveTo(0, 0);
myDrawContainer.graphics.moveTo(500, 500); //-> RESULT: Textured Line WITH Transparency 0.3!
}
你能不能请张贴实际的(相关)的代码,这将有助于我们帮助你好得多,艾莫。 – DodgerThud 2014-10-29 09:27:25
好的!我将发布代码(参见上文)! – 2014-10-29 09:57:00
好吧,我认为问题如下。改变'myLoader.content'的alpha值不会改变加载的png文件的'bitmapdata'属性。你只是改变对象的flash内部alpha值。因此,当您传递加载文件的内容时,它仍然具有与以前相同的位图数据。现在,我有一个问题,为什么不直接将myDrawContainer的alpha值设置为您需要的值,如'myDrawContainer.alpha = 0.6'? – DodgerThud 2014-10-29 10:06:27