package { import flash.display.Bitmap; import flash.display.Loader; import flash.display.LoaderInfo; import flash.display.Sprite; import flash.events.Event; import flash.net.URLRequest; public class CDTimeSample extends Sprite { private var cdMask :CDMask; public function CDTimeSample() { init(); } private function init():void { loadImg(); } private function loadImg():void { var imgLoader :Loader = new Loader(); var url:String = "1.png"; imgLoader.contentLoaderInfo.addEventListener(Event.COMPLETE,imgLoaderHandler); imgLoader.load(new URLRequest(url)); } private function imgLoaderHandler(e:Event):void { var imgBMP :Bitmap = (((e.target) as LoaderInfo).content as Bitmap); cdMask = new CDMask(imgBMP.bitmapData); addChild(cdMask); } } } package { import flash.display.Bitmap; import flash.display.BitmapData; import flash.display.Shape; import flash.display.Sprite; import flash.events.Event; import flash.events.MouseEvent; import flash.filters.ColorMatrixFilter; import flash.utils.getTimer; import flash.geom.Matrix; import flash.sampler.getSize; /** *冷却时间遮罩类 **/ public class CDMask extends Sprite { private var icon :Shape; //图标 private var cdMask :Sprite; //冷却时间遮罩 private var currentAngle :Number; //当前的角度 private var speed :Number; //CD时间速度 private var radius :Number; //遮罩半径 //private var background :Bitmap; //背景图 private var startTime :Number; //开始时间 private var totalTime :Number = 500; //CD冷却时间,1秒 private function drawBitmap(bgshape:Shape,bmp:BitmapData,_alpha:Number = 1.0):void { bgshape.graphics.beginBitmapFill(bmp); bgshape.graphics.drawRect(0, 0, bmp.width, bmp.height); bgshape.graphics.endFill(); bgshape.alpha = _alpha; } public function CDMask(BMD :BitmapData) { super(); var bg:Shape = new Shape(); drawBitmap(bg,BMD,0.2); addChild(bg); this.icon = new Shape(); // new Bitmap(BMD); drawBitmap(icon,BMD,1.0); addChild(icon); /* var background:Bitmap = new Bitmap(BMD); //this.background.filters = [new ColorMatrixFilter([1,0,0,0,0,1,0,0,0,0,1,0,0,0,0,0,0,0,1,0])]; //灰色滤镜数组 background.alpha = 0.2; addChild(background); trace(getSize(bgshape),getSize(background)); */ this.addEventListener(MouseEvent.CLICK, startCDHandler); } private function startCDHandler(e:MouseEvent):void { startTime = getTimer(); //开始获取时间 cdMask = new Sprite(); this.addChild(cdMask); icon.mask = cdMask; currentAngle = 0; speed = 1; radius = Math.sqrt(this.width/2 * this.width/2 + this.height/2 * this.height/2); //遮罩半径,勾股定理,你们都懂的 cdMask.x = this.width/2; cdMask.y = this.height/2; cdMask.graphics.beginFill(0); cdMask.graphics.lineTo(radius,0); cdMask.addEventListener(Event.ENTER_FRAME , efHandler); removeEventListener(MouseEvent.CLICK,startCDHandler); } private function efHandler(e:Event):void { var postTime :Number = getTimer() - startTime; //程序运行到这里的时间-开始的时间 currentAngle += speed; if(postTime <= totalTime) { var currAngle:Number = 2 * Math.PI * ( postTime / totalTime );//根据经过时间算出弧度 var toX :Number = radius * Math.cos(currAngle); var toY :Number = radius * Math.sin(currAngle); cdMask.graphics.lineTo(toX,toY); } else { if (this.contains(cdMask)) { this.removeChild(cdMask); icon.mask = null; this.addEventListener(MouseEvent.CLICK, startCDHandler); removeEventListener(Event.ENTER_FRAME, efHandler); } } } } }
var _s:CDTimeSample = new CDTimeSample();
addChild(_s);
时间: 2024-11-06 20:49:05