此文出現源於忽略PNG透明區域的事件(Flex)...
下面是純as的實現..用的是Loader..幫羣裏的朋友寫的實例
詳情的思路什麼的就不說了..看上面的地址
效果(左邊的PNGLoader加截的png忽略透明,右邊的爲普通Loader):
代碼:
PNGLoader.as
01.
package
net.l4cd.display
02.
{
03.
import
flash.display.BitmapData;
04.
import
flash.display.Loader;
05.
import
flash.display.Sprite;
06.
import
flash.events.Event;
07.
import
flash.events.IOErrorEvent;
08.
import
flash.events.ProgressEvent;
09.
import
flash.geom.Matrix;
10.
import
flash.net.URLRequest;
11.
import
flash.system.LoaderContext;
12.
import
flash.utils.ByteArray;
13.
14.
/**
15.
* PNGLoader,主要解決png圖片透明像素處事件的問題
16.
* @author L4cd.Net
17.
*
18.
*/
19.
public
class
PNGLoader
extends
Sprite
20.
{
21.
private
var
loader:Loader =
new
Loader();
22.
private
var
hit:Sprite =
new
Sprite();
23.
public
function
PNGLoader()
24.
{
25.
addChild(loader);
26.
addChild(hit);
27.
hit.visible =
false
;
28.
hit.mouseEnabled =
false
;
29.
mouseChildren =
false
;
30.
hitArea = hit;
31.
loader.contentLoaderInfo.addEventListener(Event.COMPLETE,complete);
32.
loader.contentLoaderInfo.addEventListener(IOErrorEvent.IO_ERROR,error);
33.
loader.contentLoaderInfo.addEventListener(ProgressEvent.PROGRESS,progress);
34.
}
35.
private
function
complete(e:Event):
void
36.
{
37.
dispatchEvent(e);
38.
update();
39.
}
40.
private
function
error(e:IOErrorEvent):
void
41.
{
42.
dispatchEvent(e);
43.
}
44.
private
function
progress(e:ProgressEvent):
void
45.
{
46.
dispatchEvent(e);
47.
}
48.
public
function
load(request:URLRequest,context:LoaderContext=
null
):
void
49.
{
50.
loader.load(request,context);
51.
clear();
52.
}
53.
public
function
loadBytes(bytes:ByteArray,context:LoaderContext=
null
):
void
54.
{
55.
loader.loadBytes(bytes,context);
56.
clear();
57.
}
58.
public
function
unload():
void
59.
{
60.
loader.unload();
61.
clear();
62.
}
63.
public
function
close():
void
64.
{
65.
loader.close();
66.
clear();
67.
}
68.
private
function
clear():
void
69.
{
70.
hit.graphics.clear();
71.
}
72.
private
function
update():
void
73.
{
74.
if
(!loader.content)
return
;
75.
var
bit:BitmapData =
new
BitmapData(loader.width,loader.height,
true
,
0x00000000
);
76.
bit.draw(loader);
77.
//重繪圖象到bit
78.
clear();
79.
hit.graphics.beginFill(
0
);
80.
for
(
var
x:
uint
=
0
;x<bit.width;x++)
81.
{
82.
for
(
var
y:
uint
=
0
;y<bit.height;y++)
83.
{
84.
if
(bit.getPixel32(x,y))hit.graphics.drawRect(x,y,
1
,
1
);
85.
}
86.
}
87.
//以graphics畫出bit的無透明區域
88.
hit.graphics.endFill();
89.
}
90.
}
91.
}
PNGLoaderExample.as
01.
package
02.
{
03.
import
flash.display.Loader;
04.
import
flash.display.Sprite;
05.
import
flash.events.MouseEvent;
06.
import
flash.filters.GlowFilter;
07.
import
flash.net.URLRequest;
08.
09.
import
net.l4cd.display.PNGLoader;
10.
11.
12.
[SWF(width=
"600"
,height=
"400"
)]
13.
/**
14.
* PNGLoaderExample
15.
* @author L4cd.Net
16.
*
17.
*/
18.
public
class
PNGLoaderExample
extends
Sprite
19.
{
20.
public
function
PNGLoaderExample()
21.
{
22.
var
pl:PNGLoader =
new
PNGLoader();
23.
pl.load(
new
URLRequest(
"10020601.png"
));
24.
addChild(pl);
25.
pl.y =
20
;
26.
pl.addEventListener(MouseEvent.ROLL_OUT,o);
27.
pl.addEventListener(MouseEvent.ROLL_OVER,o);
28.
29.
var
ld:Loader =
new
Loader();
30.
ld.load(
new
URLRequest(
"10020601.png"
));
31.
addChild(ld);
32.
ld.x =
320
;
33.
ld.y =
20
;
34.
ld.addEventListener(MouseEvent.ROLL_OUT,o);
35.
ld.addEventListener(MouseEvent.ROLL_OVER,o);
36.
37.
38.
graphics.lineStyle(
1
);
39.
graphics.drawRect(
20
,
20
,
250
,
250
);
40.
41.
graphics.drawRect(
320
,
20
,
250
,
250
);
42.
}
43.
private
function
o(e:MouseEvent):
void
44.
{
45.
e.target.filters = (e.type == MouseEvent.ROLL_OVER)?[
new
GlowFilter()]:[];
46.
}
47.
}
48.
}