[ActionScript 3.0] 利用InteractivePNG.as类精确选择识别png图片有像素的区域

用法:如果是把png直接导入flash转换成影片剪辑,只需在影片剪辑属性中勾选为ActionScript导出(x),并把基类里的flash.display.MovieClip替换成InteractivePNG即可;如果是外部导入png,只需将存放png的类继承InteractivePNG即可;

 1 package 
 2 {
 3     import flash.display.Loader;
 4     import flash.display.MovieClip;
 5     import flash.display.Shape;
 6     import flash.display.Sprite;
 7     import flash.events.Event;
 8     import flash.events.MouseEvent;
 9     import flash.filters.DropShadowFilter;
10     import flash.net.URLRequest;
11 
12     /**
13       * ...
14       * @author Frost.Yen
15       */
16     public class TestPNG extends Sprite
17     {
18         private var pngSp:PNGContainer;
19         private var ldr:Loader = new Loader  ;
20         private var lineSp:Sprite;
21         private var frame:Shape = new Shape  ;
22         public function TestPNG()
23         {
24 
25             lineSp = new Sprite  ;
26             pngSp = new PNGContainer  ;
27             pngSp.x = pngSp.y = 200;
28             this.addChild(lineSp);
29             this.addChild(pngSp);
30             pngSp.addEventListener(MouseEvent.MOUSE_OVER,onPNGOver);
31             pngSp.addEventListener(MouseEvent.MOUSE_OUT,onPNGOut);
32         }
33 
34         private function onPNGOver(e:MouseEvent):void
35         {
36             drawLine(pngSp);
37         }
38 
39         private function onPNGOut(e:MouseEvent):void
40         {
41             this.removeChild(frame);
42         }
43 
44         private function drawLine(_mc:MovieClip)
45         {
46             frame.graphics.clear();
47             frame.graphics.moveTo(_mc.x,_mc.y);
48             frame.graphics.lineStyle(1,0xff00ff);
49             frame.graphics.lineTo(_mc.x + _mc.width,_mc.y);
50             frame.graphics.moveTo(_mc.x + _mc.width,_mc.y);
51             frame.graphics.lineTo(_mc.x + _mc.width,_mc.y + _mc.height);
52             frame.graphics.moveTo(_mc.x + _mc.width,_mc.y + _mc.height);
53             frame.graphics.lineTo(_mc.x,_mc.y + _mc.height);
54             frame.graphics.moveTo(_mc.x,_mc.y + _mc.height);
55             frame.graphics.lineTo(_mc.x,_mc.y);
56             addChild(frame);
57         }
58     }
59 }
 1 package 
 2 {
 3     import flash.display.Loader;
 4     import flash.events.Event;
 5     import flash.net.URLRequest;
 6 
 7     /**
 8       * ...
 9       * @author Frost.Yen
10       */
11     public class PNGContainer extends InteractivePNG
12     {
13         private var ldr:Loader = new Loader  ;
14         public function PNGContainer()
15         {
16             ldr.load(new URLRequest("128.png"));
17             ldr.contentLoaderInfo.addEventListener(Event.COMPLETE,onPNGLoaded);
18         }
19 
20         private function onPNGLoaded(e:Event):void
21         {
22             this.addChild(e.target.content);
23         }
24     }
25 
26 }

InteractivePNG.as类可以到http://yan_frost.download.csdn.net/下载

posted on 2015-06-01 16:07  晏过留痕  阅读(556)  评论(0编辑  收藏  举报