BitMapData位图相交区域透明 解决深度管理方案

     BitMapData位图相交区域透明  解决深度管理方案

    在AS3中一般我们处理物件在场景的深度问题,都是根据物件的深度值来对物件的层级进行设置,来解决物件之间的遮挡关系.其实还可以用另一种方式来实现,使性能方面得以提升.原理其实也不复杂,就是利用位图数据绘制,并将物件之间遮挡区域进行处理,让相交区域根据物件的深度关系设置为透明和完全不透明,以达到用户视觉上的遮挡效果.当然也可以给相交区域半透明化的效果.

    下面请看实现代码:

<textarea cols="99" rows="16" name="code" class="java">&lt;?xml version="1.0" encoding="utf-8"?&gt; &lt;!-- 位图数据透明处理 实现深度排序 --&gt; &lt;mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute" minWidth="955" minHeight="600" creationComplete="init()" mouseMove="onMouseMoving(event)"&gt; &lt;mx:Script&gt; &lt;!--[CDATA[ import mx.core.BitmapAsset; [Embed(source='assets/house.png')] private var HOUSE_IMG:Class; [Embed(source='assets/avatar.png')] private var FISH_IMG:Class; private var house:BitmapAsset = new HOUSE_IMG() as BitmapAsset; private var fish:BitmapAsset = new FISH_IMG() as BitmapAsset; private var houseBd:BitmapData; private var fishBd:BitmapData; private var rect:Rectangle; private var pt:Point = new Point(0, 0); private var filter:BitmapData; private var houseBm:Bitmap; private var fishBm:Bitmap; private function init():void { houseBd = new BitmapData(house.width, house.height, true, 0x00000000); houseBd.draw(house); fishBd = new BitmapData(fish.width, fish.height, true, 0x00000000); fishBd.draw(fish); rect = new Rectangle(0, 0, fishBd.width, fishBd.height); filter = new BitmapData(fishBd.width, fishBd.height); houseBm = new Bitmap(houseBd); houseBm.x=400; houseBm.y=250; ui.addChild(houseBm); fishBm = new Bitmap(fishBd.clone()); ui.addChild(fishBm); } private function onMouseMoving(event:MouseEvent):void { // move bmp2 to the mouse position (centered). fishBm.x = mouseX - 20; fishBm.y = mouseY - 50; //得到鱼相对于房子的矩形区域 var rect2:Rectangle = new Rectangle(fishBm.x-houseBm.x, fishBm.y-houseBm.y, fishBd.width, fishBd.height); //用0xff0000ff颜色进行填充 滤镜位图数据 filter.fillRect(rect, 0xff0000ff); var color:uint; if(fishBm.y&gt;houseBm.y-100) { color=0xffffffff; } else { color=0x00000000; } //var color:uint=int(colorStr); trace(color.toString(16)); //将相交区域设置透明度 filter.threshold(houseBd, rect2, pt, "&gt;", 0x00000000,color ); //完全透明 0x00000000 完全不透明 0xffffffff 透明度 0x80000000 //bgm.bitmapData = filter; fishBm.bitmapData.copyPixels(fishBd, rect, pt, filter, pt, false); } ]]--&gt; &lt;/mx:Script&gt; &lt;mx:UIComponent id="ui"/&gt; &lt;/mx:Application&gt; </textarea>

你可能感兴趣的:(BitMapData位图相交区域透明 解决深度管理方案)