Actionscript 3 as3-设置纹理alpha值
对于ActionScript3“绘图应用程序”,我希望能够选择纹理并设置其透明度。 因此,我尝试设置纹理的alpha透明度。 但它不起作用 我的工作:Actionscript 3 as3-设置纹理alpha值,actionscript-3,actionscript,alpha-transparency,Actionscript 3,Actionscript,Alpha Transparency,对于ActionScript3“绘图应用程序”,我希望能够选择纹理并设置其透明度。 因此,我尝试设置纹理的alpha透明度。 但它不起作用 我的工作: 首先,我使用graphics.linestyle()设置线条的厚度和ALPHA值 之后,我(a)加载png,(b)读取它的位图数据,(c)然后将其与lineBitmapStyle一起使用 结果: 绘制线(使用moveTo、lineTo等)时,线使用纹理,但忽略使用lineStyle设置的“Alpha” 我做错了什么 myLoader.conten
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”,而是
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!
}
啊,我明白了,这比我最初想象的要复杂一些 好的,查看for
lineBitmapStyle
显示函数需要以下参数:lineBitmapStyle(位图:BitmapData,矩阵:矩阵=null,重复:布尔=true,平滑:布尔=false)
现在,matrix、repeat和smooth在这里对我们没有帮助(matrix在这里用于变换,即定位、旋转等),但bitmap:BitmapData可能会有帮助。我们需要做的是在将加载的PNG文件传递到lineBitmapStyle
之前操作其BitmapData。遗憾的是,我们无法直接在BMD上设置alpha值,因此我们可以尝试colorTransform
it
这是未经测试的代码,但我认为这是正确的方法:
..
//store the bitmapdata in a seperate local variable
var bmd:BitmapData = LoaderInfo(e.target).content;
//create a ColorTransform Object to change the values of the BMD
var cTransform:ColorTransform = new ColorTransform();
//now here I am unsure, manipulating the alpha value of the BMD
cTransform.alphaMultiplier = 0.6;
//defining the rectangle dimensions of the bmd, we want it to be over the entire texture
var rect:Rectangle = new Rectangle(0,0,bmd.width,bmd.height);
//apply the colorTransformation on the BMD
bmd.colorTransform(rect,cTransform);
...
//the now manipulated BMD gets set as lineBitmapStyle
myDrawContainer.graphics.lineBitmapStyle(bmd);
现在我想了想,也许我们可以解决在BMD上设置alpha值的问题,首先创建一个位图,在那里设置alpha值,然后使用位图的bitmapdata。像这样:
var bmd:BitmapData = LoaderInfo(e.target).content;
var bm:Bitmap = new Bitmap(bmd);
bm.alpha = 0.6;
myDrawContainer.graphics.lineBitmapStyle(bm.bitmapData);
好的,上面的第一个代码片段似乎就是这样做的,但是BitmapData的
透明的值必须为true。考虑到您自己不直接创建BitmapData,并且该值为false,我们在这里遇到了一个相当棘手的情况
另一种方法是创建一个额外的位图数据,以允许透明性和draw()
加载图像的位图数据:
var bmdSource:BitmapData = LoaderInfo(e.target).content;
var bmd:BitmapData = new BitmapData(bmdSource.width, bmdSource.height,true,0xffffffff);
var cTransform:ColorTransform = new ColorTransform();
cTransform.alphaMultiplier = 0.6;
var rect:Rectangle = new Rectangle(0,0,bmd.width,bmd.height);
bmd.colorTransform(rect,cTransform);
//now we have a completely white bitmapdata bmd, with an alpha value of 0.6
//we draw the contents of the bmdSource onto bmd, the alpha value effect should carry over
bmd.draw(bmdSource);
请你发布实际的(相关的)代码,这将帮助我们更好地帮助你,imo。好的!我会发布代码(见上文)!好的,我认为问题如下。更改myLoader.content
的alpha值不会更改加载的png文件的bitmapdata
的属性。您仅仅是在更改对象的内部alpha值。因此,当您传递加载文件的内容时,它仍然具有与以前相同的位图数据。现在,我有一个问题,为什么不直接将myDrawContainer的alpha值设置为所需的值,如myDrawContainer.alpha=0.6
?不幸的是,这并不能解决问题,因为这只为整个图形提供一个alpha设置。但是:当使用“普通”颜色绘制直线(使用“线型”)时,您可以使用许多不同的Alpha设置绘制直线/路径(通过在绘制特定线段之前为直线的每个线段应用不同的“线型”-命令)。这正是我在这里需要做的。我需要为不同线段绘制具有不同Alpha设置的纹理线/路径。还有其他建议吗?不幸的是,这两种方法都不起作用。第二个似乎没有任何效果。第一个选项使纹理变暗(较低的alpha值=较暗的纹理)。知道为什么吗?嗯,对不起,不知道。在我的代码片段中创建了bmd
之后,你能跟踪一下transparent
上的transparent
属性吗。这是一个只读属性,但这将告诉我们bitmapdata是否支持透明性。我们似乎接近了:当我跟踪它时,它返回FALSE。所以bitmapdata似乎还不支持透明性。但我不知道如何“激活”透明度。我知道我可以在创建带有“new”的bitmapdata对象时应用透明度(=>new bitmapdata(width,height,true)。但正如您在我的代码中看到的,我不会创建带有“new”的bitmapdata对象,而是通过从bitmap(LoaderInfo(e.target).content)返回bitmapdata来创建.bitmapData。->并且返回的bitmapData…已关闭透明度(可能我使用的图片还没有透明度。但正如我所说的:我希望能够设置透明度,即使在还没有透明度的图片上也是如此。)那么有没有办法“激活”透明度?或者我可以用“新建”创建一个新的(空)位图然后将位图的内容(LoaderInfo(e.target).content.bitmapData)添加到此中,而不覆盖透明激活的bitmapData对象??您可以尝试添加alpha通道