對於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