2014-10-29 122 views
2

对于Actionscript 3“绘图应用程序”,我希望能够选择纹理并设置它的透明度。 因此我尝试设置纹理的透明度。 但它不工作。as3 - 设置纹理alpha值

我做什么:

  1. 起初我用graphics.linestyle()来设置线的厚度和ALPHA值。
  2. 之后,我(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通道,您不使用 “线型” 可是......

  1. 创建一个新的ColorTransform对象

  2. 然后设置它的“alphaMultiplier” -attribute具体alpha通道

  3. 然后将此通过使用加载的BitmapData的“colorTransform”方法将新创建的colorTransform-Object添加到加载的BitmapData中。

但是:

这并不没有一个alpha通道或没有自己的alpha通道激活影像工作。降低Alpha通道时,这些图像只会变黑。在这种情况下,你必须这样做:

  1. 起初,我创造新的BitmapData-对象与“新”,设置其加载的图像的宽度和高度,宽度和高度,并设置第三个参数为TRUE( =透明度:开)。所以你得到了一个具有ACTIVATED透明度的“容器”。
  2. 然后,您在此“容器” - 对象上使用“copyPixels”来填充LOADED BitmapData-Object的像素。
  3. 而在此之后,上述方法与“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!   
} 
+0

你能不能请张贴实际的(相关)的代码,这将有助于我们帮助你好得多,艾莫。 – DodgerThud 2014-10-29 09:27:25

+0

好的!我将发布代码(参见上文)! – 2014-10-29 09:57:00

+0

好吧,我认为问题如下。改变'myLoader.content'的alpha值不会改变加载的png文件的'bitmapdata'属性。你只是改变对象的flash内部alpha值。因此,当您传递加载文件的内容时,它仍然具有与以前相同的位图数据。现在,我有一个问题,为什么不直接将myDrawContainer的alpha值设置为您需要的值,如'myDrawContainer.alpha = 0.6'? – DodgerThud 2014-10-29 10:06:27

回答

1

我明白了,这是一个比较复杂的,比我最初以为。

好吧,看着documentationlineBitmapStyle显示我的函数需要下列参数:lineBitmapStyle(bitmap:BitmapData, matrix:Matrix = null, repeat:Boolean = true, smooth:Boolean = false)

现在,矩阵,重复并顺利将不能帮助我们在这里(矩阵这里用于转化,即定位,旋转等),但位图:BitmapData可能。我们需要做的是在将它传递到lineBitmapStyle之前操作加载的PNG文件的BitmapData。可悲的是我们不能直接在BMD上设置alpha值,所以我们可以尝试colorTransform它。

这是未经测试的代码,但我认为这是正确的做法:

.. 
//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值,然后使用Bitmap的bitmapdata。就像这样:

var bmd:BitmapData = LoaderInfo(e.target).content; 
var bm:Bitmap = new Bitmap(bmd); 
bm.alpha = 0.6; 
myDrawContainer.graphics.lineBitmapStyle(bm.bitmapData); 

好了,从上面的第一个片段似乎是做到这一点的方式,但中BitmapData的transparent值必须是真实的。考虑到你不是直接自己创建BitmapData,并且该值是假的,我们在这里有一个相当棘手的情况。

另一种方法是创建一个额外的BitmapData允许transparancy和draw()加载的图像就可以了的BitmapData:

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); 
+0

不幸的是这两种方法都不起作用。第二个似乎没有任何影响。第一个使纹理DARKER(较低的alpha值=较深的纹理)。任何想法为什么。 – 2014-10-29 12:16:45

+0

对不起,没有。您可以在我的代码片段创建'bmd'之后跟踪'bmd'上的​​'transparent'属性。这是一个只读属性,但是这会告诉我们该位图数据是否甚至支持透明。 – DodgerThud 2014-10-29 12:29:43

+0

我们似乎很接近:当我追踪它时,它返回FALSE。所以位图数据似乎不支持透明度。但我不知道一种“激活”透明度的方法。我知道我可以在创建一个带有“new”(=> new bitmapdata(width,height,true)的位图数据对象时应用透明度。但正如您在我的代码中看到的,我不创建位图数据对象与“新”,但通过从位图(LoaderInfo(e.target)。内容).bitmapData返回位图数据。 - >和返回的BitmapData ... – 2014-10-29 13:36:00