【发布时间】:2014-10-29 09:25:58
【问题描述】:
对于 Actionscript 3“绘图应用程序”,我希望能够选择纹理并设置其透明度。 因此,我尝试设置纹理的 alpha 透明度。 但它不起作用。
我的工作:
- 首先我使用 graphics.linestyle() 来设置线条粗细和 ALPHA 值。
- 之后,我 (a) 加载一个 png,(b) 读取它的 bitmapData,(c) 然后将它与 lineBitmapStyle 一起使用。
结果:
当绘制线条(使用 moveTo、lineTo 等)时,线条使用纹理,但忽略使用 lineStyle 设置的“Alpha”。
我做错了什么?
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 通道,您不使用“lineStyle”,但...
创建一个新的颜色变换对象
然后将其设置为“alphaMultiplier”-属性到特定的 alphaChannel
然后通过使用加载的 BitmapData 的“colorTransform”-Method 将这个新创建的 colorTransform-Object 应用到加载的 BitmapData。
但是:
- 首先,我使用“new”创建新的 BitmapData-Object,将其宽度和高度设置为加载图像的宽度和高度,并将第三个参数设置为 TRUE(= 透明度:ON)。所以你得到了一个“容器”,它具有激活的透明度。
- 然后在这个“Container”-Object 上使用“copyPixels”,用 LOADED BitmapData-Object 的像素填充它。
- 在此之后,使用“colorTransform”-Object 的上述方法带来了预期的结果。
下面是工作代码:
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!
}
【问题讨论】:
-
能否请您发布实际(相关)代码,这将帮助我们更好地帮助您,imo。
-
好的!我会发布代码(见上文)!
-
好吧,我认为问题如下。更改
myLoader.content的 alpha 值不会更改加载的 png 文件的bitmapdata的属性。您只是在更改对象的 flash 内部 alpha 值。因此,当您传递加载文件的内容时,它仍然具有与以前相同的位图数据。现在,我有一个问题,为什么不直接将 myDrawContainer 的 alpha 值设置为您需要的值,例如myDrawContainer.alpha = 0.6? -
不幸的是,这并不能解决问题,因为它只为整个绘图提供了一个 Alpha 设置。但是:当用“正常”颜色(使用“lineStyle”)绘制一条线时,您可以使用许多不同的 Alpha 设置绘制一条线/路径(通过为线的每一段应用不同的“lineStyle”命令就在绘制特定段之前)。这正是我需要在这里做的。我需要为线的不同段绘制具有不同 Alpha 设置的纹理线/路径。还有其他建议吗?
标签: actionscript-3 actionscript alpha-transparency