2014-10-29 75 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