用法:如果是把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.Yen15 */16 public class TestPNG extends Sprite17 {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):void35 {36 drawLine(pngSp);37 }38 39 private function onPNGOut(e:MouseEvent):void40 {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.Yen10 */11 public class PNGContainer extends InteractivePNG12 {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):void21 {22 this.addChild(e.target.content);23 }24 }25 26 }
InteractivePNG.as类可以到下载