読み込んだ画像の重ね合わせ
開発環境:FlashDevelop 4.0.4
プロジェクト:AIR AS3 Projector
確認端末:Win XP
テストとかサンプルの確認するのにいちいち携帯端末を触ってられないのでPCで確認した。
ゲームとかの画像を表示したかった、背景.pngを用意して、キャラクター.pngを用意して
プログラムを書いて実行したけど、キャラクター.pngの周辺の色が透過しなくて悩んだのでメモ。
画像(透過PNG)は下記のフォルダに保存した。
/bin/img/Red.png
/bin/img/Blue.png
透過の対象色:(R:255,G:128,B:192)
copyPixelsで第6引数をtrueにすると、透過される。
bgroundBitmapData.copyPixels(redBitmapData, rect, pos, null, null, true);
透過PNGでない場合はthresholdを使う。
0xffff80c0(R:255,G:128,B:192)の色を、0x00000000(透明)にする。
redBitmapData.threshold(redBitmapData, redBitmapData.rect, new Point(0, 0), "==", 0xffff80c0, 0x00000000, 0xffffffff);
面倒でなければ、透過PNGにした方がコードが1行少なくなるから良いかな?
Main.as
package { import flash.display.Bitmap; import flash.display.BitmapData; import flash.display.Loader; import flash.display.Sprite; import flash.events.Event; import flash.geom.Point; import flash.geom.Rectangle; import flash.net.URLRequest; [SWF(width = "400", height = "400")] /** * ... * @author example */ public class Main extends Sprite { public var bgroundBitmap:Bitmap; public var bgroundBitmapData:BitmapData; public var redLoader:Loader; public var redBitmap:Bitmap; public var redBitmapData:BitmapData; public var blueLoader:Loader; public var blueBitmap:Bitmap; public var blueBitmapData:BitmapData; public function Main():void { bgroundBitmapData = new BitmapData(stage.stageWidth, stage.stageHeight, true, 0x00000000); bgroundBitmap = new Bitmap(); addChild(bgroundBitmap); redLoader = new Loader(); redLoader.load(new URLRequest("./img/Red.png")); redLoader.contentLoaderInfo.addEventListener(Event.INIT, redInitHandler); blueLoader = new Loader(); blueLoader.load(new URLRequest("./img/Blue.png")); blueLoader.contentLoaderInfo.addEventListener(Event.INIT, blueInitHandler); } public function redInitHandler(e:Event):void { redBitmapData = new BitmapData(redLoader.width, redLoader.height, true, 0x00000000); redBitmapData.draw(redLoader); //redBitmapData.threshold(redBitmapData, redBitmapData.rect, new Point(0, 0), "==", 0xffff80c0, 0x00000000, 0xffffffff); var pos:Point = new Point(0, 0); var rect:Rectangle = new Rectangle(0, 0, redBitmapData.width, redBitmapData.height); bgroundBitmapData.copyPixels(redBitmapData, rect, pos, null, null, true); bgroundBitmap.bitmapData = bgroundBitmapData; } public function blueInitHandler(e:Event):void { blueBitmapData = new BitmapData(blueLoader.width, blueLoader.height, true, 0x00000000); blueBitmapData.draw(blueLoader); //blueBitmapData.threshold(blueBitmapData, blueBitmapData.rect, new Point(0, 0), "==", 0xffff80c0, 0x00000000, 0xffffffff); var pos:Point = new Point(50, 50); var rect:Rectangle = new Rectangle(0, 0, blueBitmapData.width, blueBitmapData.height); bgroundBitmapData.copyPixels(blueBitmapData, rect, pos, null, null, true); bgroundBitmap.bitmapData = bgroundBitmapData; } } }
実行結果