AS3 纯代码打造不规则遮罩,透明区域不操作

来源:互联网 发布:阿里云学生认证 高中生 编辑:程序博客网 时间:2024/05/16 00:39

1.遮罩的用处

flash 中 遮罩只要你思想很奇妙,可以做出很多事情来. 我最常用的就是利用遮罩,屏蔽 某个物体的透明部分
在实际例子运用中, 比如涂色游戏,  在一个不规则并有透明色的物体上, 按下开始涂色, 但是不能涂抹掉原来就是透明的颜色, 也不能整个矩形都填充起来
一般可以把这个 物体 复制到另一个图层, 右键另一个图层设置为 遮罩就行了.  (被遮罩物体图层中只能存在这一个物体,不能有其他杂物)


2.AS3中运用遮罩

其中运用的就是 mark 属性.
下面先列出代码

import flash.display.*;
// 遮罩位图数据
private var markData:BitmapData;
// 遮罩private var marks:MovieClip = new MovieClip;
<pre name="code" class="cpp">//_parrnt = 需要遮罩的影片剪辑

_parrnt.cacheAsBitmap = true;markData = new BitmapData(_parrnt.width, _parrnt.height, true, 0);markData.draw(_parrnt);
<span style="white-space:pre"></span>marks.graphics.beginFill(0xffffff);<span style="white-space:pre"></span>marks.graphics.lineStyle(0.1, 0xffffff);<span style="white-space:pre"></span>// 获取主色<span style="white-space:pre"></span>var mainColor:uint =  markData.getPixel(markData.width/2, markData.height/2);<span style="white-space:pre"></span>for (var j:uint = 0; j < markData.height; j++ ) {for (var i:uint = 0; i < markData.width; i++ ) {<span style="white-space:pre"></span><span style="white-space:pre"></span>var color:uint = markData.getPixel32(i, j);<span style="white-space:pre"></span>var alphaValue:uint = color >> 24 & 0xFF;<span style="white-space:pre"></span><span style="white-space:pre"></span>if ( alphaValue == 0xFF  ) {<span style="white-space:pre"></span><span style="white-space:pre"></span>marks.graphics.drawCircle(i, j, 1);<span style="white-space:pre"></span>}<span style="white-space:pre"></span><span style="white-space:pre"></span><span style="white-space:pre"></span><span style="white-space:pre"></span><span style="white-space:pre"></span><span style="white-space:pre"></span>}}<span style="white-space:pre"></span><span style="white-space:pre"></span>marks.cacheAsBitmap = false;<span style="white-space:pre"></span>parent.cacheAsBitmap = false;<span style="white-space:pre"></span><span style="white-space:pre"></span>marks.graphics.endFill();<span style="white-space:pre"></span>parent.mask = marks;<span style="white-space:pre"></span>parent.addChild(marks);
下班了,明天补上 - -

0 0
原创粉丝点击