[转]忽略PNG透明区域的事件(AS/Flash)

[url]http://l4cd.net/blog/post-ignore-the-events-of-png-transparency.html[/url]


此文出现源于忽略PNG透明区域的事件(Flex)...
下面是纯as的实现..用的是Loader..帮群里的朋友写的实例
详情的思路什么的就不说了..看上面的地址

效果(左边的PNGLoader加截的png忽略透明,右边的为普通Loader):




package net.l4cd.display
{
import flash.display.BitmapData;
import flash.display.Loader;
import flash.display.Sprite;
import flash.events.Event;
import flash.events.IOErrorEvent;
import flash.events.ProgressEvent;
import flash.geom.Matrix;
import flash.net.URLRequest;
import flash.system.LoaderContext;
import flash.utils.ByteArray;

/**
* PNGLoader,主要解决png图片透明像素处事件的问题
* @author L4cd.Net
*
*/
public class PNGLoader extends Sprite
{
private var loader:Loader = new Loader();
private var hit:Sprite = new Sprite();
public function PNGLoader()
{
addChild(loader);
addChild(hit);
hit.visible = false;
hit.mouseEnabled = false;
mouseChildren = false;
hitArea = hit;
loader.contentLoaderInfo.addEventListener(Event.COMPLETE,complete);
loader.contentLoaderInfo.addEventListener(IOErrorEvent.IO_ERROR,error);
loader.contentLoaderInfo.addEventListener(ProgressEvent.PROGRESS,progress);
}
private function complete(e:Event):void
{
dispatchEvent(e);
update();
}
private function error(e:IOErrorEvent):void
{
dispatchEvent(e);
}
private function progress(e:ProgressEvent):void
{
dispatchEvent(e);
}
public function load(request:URLRequest,context:LoaderContext=null):void
{
loader.load(request,context);
clear();
}
public function loadBytes(bytes:ByteArray,context:LoaderContext=null):void
{
loader.loadBytes(bytes,context);
clear();
}
public function unload():void
{
loader.unload();
clear();
}
public function close():void
{
loader.close();
clear();
}
private function clear():void
{
hit.graphics.clear();
}
private function update():void
{
if(!loader.content)return;
var bit:BitmapData = new BitmapData(loader.width,loader.height,true,0x00000000);
bit.draw(loader);
//重绘图象到bit
clear();
hit.graphics.beginFill(0);
for(var x:uint=0;x<bit.width;x++)
{
for(var y:uint=0;y<bit.height;y++)
{
if(bit.getPixel32(x,y))hit.graphics.drawRect(x,y,1,1);
}
}
//以graphics画出bit的无透明区域
hit.graphics.endFill();
}
}
}




package
{
import flash.display.Loader;
import flash.display.Sprite;
import flash.events.MouseEvent;
import flash.filters.GlowFilter;
import flash.net.URLRequest;

import net.l4cd.display.PNGLoader;


[SWF(width="600",height="400")]
/**
* PNGLoaderExample
* @author L4cd.Net
*
*/
public class PNGLoaderExample extends Sprite
{
public function PNGLoaderExample()
{
var pl:PNGLoader = new PNGLoader();
pl.load(new URLRequest("10020601.png"));
addChild(pl);
pl.y = 20;
pl.addEventListener(MouseEvent.ROLL_OUT,o);
pl.addEventListener(MouseEvent.ROLL_OVER,o);

var ld:Loader = new Loader();
ld.load(new URLRequest("10020601.png"));
addChild(ld);
ld.x = 320;
ld.y = 20;
ld.addEventListener(MouseEvent.ROLL_OUT,o);
ld.addEventListener(MouseEvent.ROLL_OVER,o);


graphics.lineStyle(1);
graphics.drawRect(20,20,250,250);

graphics.drawRect(320,20,250,250);
}
private function o(e:MouseEvent):void
{
e.target.filters = (e.type == MouseEvent.ROLL_OVER)?[new GlowFilter()]:[];
}
}
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值